0
votes

What's the appropriate way of using MagicalRecord for importing data in the background and accessing the just imported data on completion? Do I have to manually save the primary keys of all imported data and pass that to the completion block in order to find them again from CoreData?

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext)
{
    NSArray *importedEntitiesArray = [MyEntity MR_importFromArray:bigResultsArray
                       inContext:localContext];
} completion:^(BOOL success, NSError *error)
{
    // How to access the imported entities from here?
    // Note we no longer have access to the saving localContext
    // so the entities in importedEntitiesArray would be invalid
}]

I realize I can do the following:

NSArray *importedEntitiesArray = [MyEntity MR_importFromArray:bigResultsArray];
[[NSManagedObjectContext MR_defaultContext] MR_saveToPersistentStoreAndWait];

But that doesn't import the data in the background.

1
How much of the data do you want to access immediately? You don't want to do a batched fetch with some predicate? - Wain
Ideally all of the just fetched data. The reason for this is that the API knows which entities to return based on server parameters, but the CoreData entities don't have an attribute that let me calculate this by a fetch predicate (well, they do, but it needs some data building, and I wanted to figure if I could avoid building this data). - Ricardo Sanchez-Saez

1 Answers

2
votes

To transfer all of the newly background loaded managed objects to another context you need to get all of the managed object ids after the background load has completed and then request all of the objects for those ids from the other context. Whatever you consider to be the primary key won't work, it has to be the managed object id. You also need to have saved the new loaded objects to the persistent store (and refreshed the context depending on what child / parent relationships you have) so that they are available in the other context.