Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magical Record saveWithBlock: not saving

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

The code below works, and I have commented out the saveWithBlock: specific parts. The executeBlock...: methods just wrap 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];
 }];*/
like image 681
arsenius Avatar asked Jan 31 '14 18:01

arsenius


1 Answers

In the end, it was my fault (Of course!). The method cardFromDictionary: was using MR_contextForCurrentThread. Passing it localContext solved the problem.

like image 177
arsenius Avatar answered Oct 24 '22 05:10

arsenius