Вопрос

The one thing that I want to achieve - detect when all entities are created and represent it in my table.

As i understood correct the completion block for the code below will be invoked when all core data entries will be created? am I right? Because when I try to findAll entities in another controller I expected that all entries are created. But findAll return empty array. Only reload controller solve this issue.

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
    NSArray *objects = responseData[@"data"];

    NSArray *array = [Team MR_importFromArray:objects inContext:localContext];

} completion:^(BOOL success, NSError *error) {
    operationSuccess(nil); // return me to controller that requested this block
}];
Это было полезно?

Решение

There is a bug in the 2.2 release of magical record that doesn't seem to save imports. Trying using version 2.1.

Другие советы

Instead, try this:

[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {

    for(NSDictionary *dictionary in objects) {
        [Team  MR_importFromObject:dictionary inContext:localContext];
    }
} completion:^(BOOL success, NSError *error) {

    NSArray *array = [NSArray arrayWithArray:[Team MR_findAll]];
    operationSuccess(array);
}

There's a known issue with saveWithBlock and importFromArray where the completion block is fired before all the data is saved (thus why you're not seeing them returned right away - see https://github.com/magicalpanda/MagicalRecord/issues/642#issuecomment-40002145). By iterating through your array with importFromObject:inContext: you're guaranteed that your completion block will only be called after all saves are done.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top