gpt4 book ai didi

ios - 魔法记录 saveWithBlock : not saving

转载 作者:可可西里 更新时间:2023-11-01 03:58:14 26 4
gpt4 key购买 nike

我在使用 [MagicalRecord saveWithBlock:completion] 方法保存我的对象时遇到了问题。当我切换到使用普通 block 时,它工作正常。使用版本 2.2develop。

下面的代码有效,我已经注释掉了 saveWithBlock: 特定部分。 executeBlock...: 方法只是包装了 dispatch_async 和 executeBlockOnMainThread: 替换了 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];
}];*/

最佳答案

最后,这是我的错(当然!)。 cardFromDictionary: 方法使用的是 MR_contextForCurrentThread。传递 localContext 解决了这个问题。

关于ios - 魔法记录 saveWithBlock : not saving,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21487155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com