0
votes

I have a PFQueryTableViewController subclass which makes a query to a Parse.com table for data with a column named messages, which is of array type.

The query returns the data, except with messages being an empty array (although there is data in the table AND in the messages field).

Below is the query which I create for the Parse data:

- (PFQuery *)queryForTable {
    PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
    [query whereKey:@"user" equalTo:[PFUser currentUser]];
    [query includeKey:@"messages"];
    return query;
}

Weird part, once I update the Parse object and save it, reloading the table data fetches the array column's data.

Any idea why it returns an empty array with the initial query?

EDIT:

NSLog() output of the query result (from - (void)objectsDidLoad: ):

objects: (
    "<Conversation: 0x7f8590c6b0b0, objectId: 5rO7Y2R5hs, localId: (null)> {\n    ACL = \"<PFACL: 0x7f8590e61ec0>\";\n    messages =     (\n    );\n    user = \"<PFUser: 0x7f8590d9d260, objectId: M88Oz4annq>\";\n}"
)

Creating the relationship upon button click:

Message *newMessage = [[Message alloc] init];
//... set message properties
[newMessage saveInBackground];

if (!self.parseObject.objectId) {
    self.parseObject = [[MyParseObject alloc] init];
    [self.parseObject setUser:[User currentUser]];
}

[[self.parseObject messages] addObject:newMessage];
[self.parseObject saveInBackground];

The parseObject class above is what I'm querying.

1
It would help if you posted more code. What code creates the relation? What code saves it? And how is queryForTable being called, so we can be sure the save has finished? - piojo
@piojo, see update. Also, queryForTable is called automatically by the controller. - kRiZ
Is your query function being called immediately after the save function? If so, the data is probably just not saved yet. Or it's partially saved, with the user object being saved but not newMessage. But there are other things that I can think of that could go wrong, but I just don't know enough about what's happening. Also, if we get into tricky stuff about which objects need to be created in which order (and when an object needs to be saved before it can be used), that's shaky ground for me. - piojo
The newMessage object gets saved. The parseObject object gets saved with the newMessage object's pointer. Just the query does not return it with the rest of the data. - kRiZ
It's a column of type array. I've followed the example in the docs under the Relations section (sub-section on using arrays for relations). - kRiZ

1 Answers

0
votes

The way you are saving your message is very time prone. As you aren't waiting for 'newMessage' to be saved yet. So it's ID value may be null.

Make sure the newMessage object is saved first, by placing the next code inside a completion block.

Message *newMessage = [[Message alloc] init];
[newMessage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if(succeeded){
        if (!self.parseObject.objectId) {
            self.parseObject = [[MyParseObject alloc] init];
            [self.parseObject setUser:[User currentUser]];
        }

        [[self.parseObject messages] addObject:newMessage];
        [self.parseObject saveInBackground];
    }
}]; 

Without seeing the definitions of your Message and MyParseObject classes, I can't be much more helpful however.