Note that there are some explanatory texts on larger screens.

plurals
  1. POMagicalRecord + AFNetworking + NSFetchedResultsController, how to make it work?
    primarykey
    data
    text
    <p>In my application I try to use MagicalRecord + AFNetworking + NSFetchedResultsController together, to synchronize data and dynamically display it on map or in tableView.</p> <p>Let's see some code for download method:</p> <pre><code>+ (void) getDataWithCompletionBlock: (void (^)(void)) block { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL_GET_DATA]]; AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ for (NSDictionary *dict in JSON) { [MyModel createOrUpdateMyModelFromDict:[dict mutableCopy]]; } [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveInBackgroundCompletion:^{ [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveNestedContexts]; block(); }]; }); } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { DDLogError(@"getDataWithCompletionBlock FAILURE: %@", error); }]; [operation start]; } </code></pre> <p>I download data using <code>AFJSONRequestOperation</code>, then create models using GCD and background thread, save context for current thread and perform <code>successBlock</code> (MagicalRecord runs <code>successBlock</code> on <code>dispatch_get_main_queue()</code>, so it is called in GUI thread.</p> <p>Is this synchronization model ok ? Because sometimes (more often on real device than simulator) I get some errors from <code>NSFetchedResultsController</code> like "no object at index: in section at index:" or "CoreData could not fulfill a fault...".</p> <p>They all suggest, there is something wrong with Core Data and multithreading environment. Did anyone try to connect all these three tools to work together ? If so, what am I missing ? Do you have any good-working code samples for that architecture ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload