简体   繁体   中英

Reloading a Core Data UITableView after iCloud update

I am currently using iCloud to sync Core Data between devices. The sync of data between devices is working but the call to reload the data of the UITableView containing the data is faulty. This is my code:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(pushListLearn:)
                                             name:@"receivedLocalNotification"
                                           object:nil];

- (void)reloadFetchedResults:(NSNotification*)note {
    NSLog(@"Underlying data changed ... refreshing!");
    [self.fetchedResultsController performFetch:nil];
}

If I leave the device updating alone, the UITableView updates successfully with the new data appearing. However, if another device deletes an object on another device while the main device is open and scrolling (reloading individual cell data), my app crashes with the following:

[__NSArrayI objectAtIndex:]: index 0 beyond bounds for empty array

Here is my UITableView configuration if you require it:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][0];
    return [sectionInfo numberOfObjects];
}

Thanks in advance!

All I needed to add to the following method is to reload the UITableView directly after the fetch:

- (void)reloadFetchedResults:(NSNotification*)note {
    NSLog(@"Underlying data changed ... refreshing!");
    [self.fetchedResultsController performFetch:nil];
    [self.tableView reloadData];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM