SaveWithBlock magic record: not saving

I'm having trouble saving my objects using the [MagicalRecord saveWithBlock: completion] method. When I switch to using regular blocks, it works fine. Using version 2.2develop.

Below is the code, and I commented on saveWithBlock: specific parts. The executeBlock ... method: the methods simply carry dispatch_async and executeBlockOnMainThread: replaces the completion block :.

[card.managedObjectContext MR_saveToPersistentStoreAndWait]; //[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) { // Replace this... [NSObject executeBlockInBackground:^{ // With this... Card *localCard = [card MR_inContext:[NSManagedObjectContext MR_contextForCurrentThread]]; // MR_inContext:localContext]; NSRange range = [localCard.fragment rangeOfString:localCard.term options:NSCaseInsensitiveSearch]; if (range.location == NSNotFound){ return; } NSString *fragmentString = [localCard.fragment stringByReplacingOccurrencesOfString:@" " withString:@"+"]; NSString *url = [NSString stringWithFormat:@"https://www.lingq.com/api/languages/%@/phrases/?word=%@&frag=%@&start=%@", appDelegate.currentLanguage, localCard.term, fragmentString, @(range.location)]; NSMutableURLRequest *request = [LQAPI requestForURL:url body:nil HTTPMethod:@"GET"]; NSData *response = [LQAPI executeURLRequest:request]; NSDictionary *responseDictionary = [LQAPI dictionaryFromJSONData:response]; if (responseDictionary == nil){ return; } NSArray *phrases = [responseDictionary objectForKey:@"phrases"]; NSMutableArray *cards = [NSMutableArray array]; int index = 0; for (NSDictionary *d in phrases){ Card *c = [self cardFromDictionary:d content_id:localCard.content_Id index:index type:BlueCard]; c.parentID = localCard.mId; // This marks it as a phrase [cards addObject:c]; index++; } [localCard.managedObjectContext MR_saveToPersistentStoreAndWait]; // This shouldn't be necessary using saveWithBlock: and it didn't help when I tried it. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parentID = %@", localCard.mId]; NSArray *phrases2 = [Card MR_findAllWithPredicate:predicate]; // All the objects appear fine here [NSObject executeBlockOnMainThread:^{ // This would have been in the completion block below NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parentID = %@", card.mId]; NSArray *phrases = [Card MR_findAllWithPredicate:predicate]; // This does find phrases [delegate didFetchPhrases:phrases forCard:card]; }]; }]; /* completion:^(BOOL success, NSError *error) { NSPredicate *predicate = [NSPredicate predicateWithFormat:@"parentID = %@", card.mId]; NSArray *phrases = [Card MR_findAllWithPredicate:predicate]; // This doesn't find any phrases [delegate didFetchPhrases:phrases forCard:card]; }];*/ 
+6
source share
2 answers

In the end, it was my mistake (of course!). The cardFromDictionary: method used MR_contextForCurrentThread. Passing this localContext solved the problem.

+3
source

On Github, I found that fetching during saveWithBlock: no path: https://github.com/magicalpanda/MagicalRecord/issues/339

0
source

All Articles