0

I've just about completed a charity app and up until now I used temporary users to populate my table view cells. I stored these user objects in an NSMutableArray (*people). The app was working fine and I'm trying to add the objects I created in parse.com to the same people array.

I've tried many different things but keep getting the error I've pasted at the bottom of this post.

How I'm populating my parse.com database with data taken from a form:

PFObject *newPerson = [PFObject objectWithClassName:@"Person"];

    NSNumber *age = [NSNumber numberWithInt:[[self mPerson] ageAtDisappearance]];
    NSNumber *active = [NSNumber numberWithInt:[[self mPerson] active]];
    NSData* data = UIImageJPEGRepresentation([[self mPerson] image], 0.5f);
    PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:data];


    newPerson[@"name"] = [[self mPerson] name];
    newPerson[@"ageAtDisappearance"] = age;
    newPerson[@"since"] = [[self mPerson] mSince];
    newPerson[@"from"] = [[self mPerson] mFrom];
    [newPerson setObject:imageFile forKey:@"image"];
    newPerson[@"active"] = active;
    [newPerson saveInBackground];

Example of how I'm locally populating my table (this code is inside my viewDidLoad method below:

Person *person = [[Person alloc] init];
[person setName:@"Ben Huddle"];
[person setNotes:@"Some text here"];
[person setAgeAtDisappearance:18];
[person setSince:[NSDate date]];
[person setFrom:@"London, SW"];
[person setReferenceNumber:@"14-334544"];
UIImage *image = [UIImage imageNamed:@"ben.png"];
[person setImage:image];
[person setActive:1];
[people addObject:person];

viewDidLoad:

    people = [[NSMutableArray alloc] init];


    PFQuery *query = [PFQuery queryWithClassName:@"Person"];
   // [query whereKey:@"active" equalTo:@0];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

        if (!error) {
            for (PFObject *object in objects) {
                 NSLog(@"%@", object);
                PFObject *person = [object objectForKey:[object objectId]];
                [people addObject:person];
            }

            dispatch_async(dispatch_get_main_queue(), ^ {
                [[self tableView] reloadData];
            });
        } else {
            // Log details of the failure
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];

My UITableView:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.

    if (tableView == [[self searchDisplayController] searchResultsTableView]) {
        return [searchResults count];

    } else {
        return [people count];
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    // Configure the cell...
    Person *current;
    if (tableView == [[self searchDisplayController] searchResultsTableView]) {
        current = [searchResults objectAtIndex:indexPath.row];
    } else {
        current = [people objectAtIndex:[indexPath row]];
    }

    [[cell textLabel] setText: [current name]];
    [[cell imageView] setImage: [current image]];
    [[cell detailTextLabel] setText: [current notes]];

    return cell;
}

Error:

2014-02-22 04:38:43.912 MPeople[28451:70b] <Person:SVeM3MnILW:(null)> {
    active = 0;
    ageAtDisappearance = 34;
    image = "<PFFile: 0xbe16880>";
    from = Eastham;
    since = "2014-02-22 00:00:00 +0000";
    name = "John Smith";
}
2014-02-22 04:38:43.914 MPeople[28451:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
    0   CoreFoundation                      0x027b85e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x0253b8b6 objc_exception_throw + 44
    2   CoreFoundation                      0x0276abcc -[__NSArrayM insertObject:atIndex:] + 844
    3   CoreFoundation                      0x0276a870 -[__NSArrayM addObject:] + 64
    4   MPeople                       0x000102fc __42-[MPPeopleTableViewController viewDidLoad]_block_invoke + 620
    5   MPeople                       0x00062667 __40-[PFTask thenCallBackOnMainThreadAsync:]_block_invoke_2 + 241
    6   libdispatch.dylib                   0x02a527f8 _dispatch_call_block_and_release + 15
    7   libdispatch.dylib                   0x02a674b0 _dispatch_client_callout + 14
    8   libdispatch.dylib                   0x02a5575e _dispatch_main_queue_callback_4CF + 340
    9   CoreFoundation                      0x0281da5e __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 14
    10  CoreFoundation                      0x0275e6bb __CFRunLoopRun + 1963
    11  CoreFoundation                      0x0275dac3 CFRunLoopRunSpecific + 467
    12  CoreFoundation                      0x0275d8db CFRunLoopRunInMode + 123
    13  GraphicsServices                    0x043399e2 GSEventRunModal + 192
    14  GraphicsServices                    0x04339809 GSEventRun + 104
    15  UIKit                               0x012a9d3b UIApplicationMain + 1225
    16  MPeople                             0x0000620d main + 141
    17  libdyld.dylib                       0x02cf970d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Thanks for your time. Kind regards

1 Answer 1

1

In your query in viewDidLoad, you should be parsing your PFObjects into Person objects before adding them one by one into your people array. The following line looks fishy because you are adding an PFObject instead to your array of Persons:

PFObject *person = [object objectForKey:[object objectId]];
[people addObject:person];

Instead, you should be parsing out the values of each PFObject into your Person object:

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (!error) {
        for (PFObject *object in objects) {
             NSLog(@"%@", object);

             Person *person = [[Person alloc] init];
             [person setName:[object objectForKey:@"name"]];
             // ... similarly, populate the rest of the Person properties from your PFObject 
             UIImage *image = [object objectForKey:@"image"];
             [person setImage:image];
             [people addObject:person];
        }

        dispatch_async(dispatch_get_main_queue(), ^ {
            [[self tableView] reloadData];
        });
    } else {
        // Log details of the failure
        NSLog(@"Error: %@ %@", error, [error userInfo]);
    }
}]
Sign up to request clarification or add additional context in comments.

2 Comments

I really don't know how I could miss that. It's working now apart from the image. Should be easy to solve thanks.
You are welcome. A quick check seems to indicate that images have to be converted to NSData, then passed into an PFFile, before setting it as a property of the PFObject. See parse.com/tutorials/saving-images

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.