lonlywolf
lonlywolf

Reputation: 505

PFObject subclassing with relationships

I have two classes:

@interface RMEvent : PFObject <PFSubclassing>
@property (nonatomic, strong) NSArray *attachments;

and

@interface RMFile : PFObject <PFSubclassing>
@property (nonatomic, strong) PFObject *event;

I'm doing this:

RMEvent *newEvent = [RMEvent object];
[newEvent pinInBackground];
RMFile *newFile = [RMFile object];
newFile.event = newEvent;
[newFile pinInBackground];
[newEvent addObject:newFile forKey:@"attachments"];

and I can see objects are created locally (tested with a query to localDatabase). Everything seems okay. But then I do (in a lot of different combinations):

[newEvent saveEventually];
[newFile saveEventually];

and I can't see anything on the server. How can I save those objects to the server? What is the correct order or maybe I'm doing something wrong in general?

P.S.: I have [self registerSubclass] in each subclass' +load method and I'm instantiating an array in the +object method of RMEvent, so it shouldn't be the case.

Upvotes: 0

Views: 173

Answers (1)

Thibaud David
Thibaud David

Reputation: 504

You should check the saveEventually callback for error

[newFile saveEventually:^(BOOL success, NSError *error) {
    if (error) NSLog(error);
}];

Is RMFile a PFFile subclass ? If so, you can't saveEventually a PFFile, and can't save an object which as a pointer to a new unsaved PFfile

Edit: "Found a circular dependency when saving"

Without cloud code :

RMEvent *newEvent = [RMEvent object];
RMFile * newFile = [RMFile object];

newEvent[@"newFile"] = newFile;
[newEvent saveInBackgroundWithBlock:^(BOOL success, NSError *error)
{
    if(!error)
    {
        newFile[@"newEvent"] = newEvent;
        [newFile saveEventually];
    }
}];

With CloudCode:

RMEvent *newEvent = [RMEvent object];
RMFile * newFile = [RMFile object];
newEvent[@"newFile"] = newFile;
[newEvent saveInBackground];

And then on a CloudCode afterSave trigger, you can retrieve your RMEvent, get 'newFile' key from it, assign a pointer to newEvent on newFile object, then save newFile

Upvotes: 1

Related Questions