ios - Magical Record saveWithBlock: not saving -
i'm having trouble getting objects save using method [magicalrecord savewithblock:completion]. when switch on using normal blocks works fine. using version 2.2develop.
the code below works, , have commented out savewithblock: specific parts. executeblock...: methods wrap dispatch_async , executeblockonmainthread: replaces completion: block.
[card.managedobjectcontext mr_savetopersistentstoreandwait]; //[magicalrecord savewithblock:^(nsmanagedobjectcontext *localcontext) { // replace this... [nsobject executeblockinbackground:^{ // 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; (nsdictionary *d in phrases){ card *c = [self cardfromdictionary:d content_id:localcard.content_id index:index type:bluecard]; c.parentid = localcard.mid; // marks phrase [cards addobject:c]; index++; } [localcard.managedobjectcontext mr_savetopersistentstoreandwait]; // shouldn't necessary using savewithblock: , didn't when tried it. nspredicate *predicate = [nspredicate predicatewithformat:@"parentid = %@", localcard.mid]; nsarray *phrases2 = [card mr_findallwithpredicate:predicate]; // objects appear fine here [nsobject executeblockonmainthread:^{ // have been in completion block below nspredicate *predicate = [nspredicate predicatewithformat:@"parentid = %@", card.mid]; nsarray *phrases = [card mr_findallwithpredicate:predicate]; // 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]; // doesn't find phrases [delegate didfetchphrases:phrases forcard:card]; }];*/
in end, fault (of course!). method cardfromdictionary: using mr_contextforcurrentthread. passing localcontext solved problem.
Comments
Post a Comment