我在添加关系后获取对象时遇到问题。我第一次获取该类别,总是找到,然后当我添加到关系时,找不到以下类别。
关系是Many-To-Many
。
示例:
categoryId = 10
如果多个类别具有相同的id
,categoryId = 10
,则找不到
NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[private setParentContext:self.model.context];
__block NSError *error = nil;
[private performBlockAndWait:^{
GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:private];
for (NSDictionary *dic in responseObject[@"response"]) {
GPCategory *category;
//The first time always found
if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:private])) {
NSLog(@"Found");
[category addDealsObject:deal];
} else {
NSLog(@"Not Found");
}
}
}];
NSError *PrivateError = nil;
if (![private save:&PrivateError]) {
NSLog(@"Unresolved error %@, %@", PrivateError, [PrivateError userInfo]);
abort();
}
if (!error) {
//Save on main moc
[self.model saveWithErrorBlock:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
}
}];
} else {
NSLog(@"Error saving context: %@\n%@", [error localizedDescription], [error userInfo]);
}
修改
解决了,我想我的问题是我忘了在每次迭代结束时保存主要上下文。
NSManagedObjectContext *backgroundMOC = [self.model backgroundMOC:self.model.context];
[backgroundMOC performBlockAndWait:^{
for (NSDictionary *dic in responseObject[@"response"]) {
GPDeal *deal = [EKManagedObjectMapper objectFromExternalRepresentation:dic withMapping:[GPDeal objectMapping] inManagedObjectContext:backgroundMOC];
GPCategory *category;
if ((category = [GPCategory MR_findFirstByAttribute:@"catId" withValue:dic[@"mainAttribute"] inContext:backgroundMOC])) {
NSLog(@"Found with mainAttribute %@", dic[@"mainAttribute"]);
[deal addDealCategoryObject:category];
}
if([backgroundMOC hasChanges]) {
NSError * error;
[backgroundMOC save:&error];
[self.model.context performBlockAndWait:^{
if([self.model.context hasChanges]) {
NSError * error;
[self.model.context save:&error];
}
}];
}
}
}];
答案 0 :(得分:1)
您可能缺乏保存MOC链。为清楚起见,我已将关键字private
替换为变量名backgroundMOC
。
关于上述问题,我只能假设行NSManagedObjectContext *private = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
在技术上类似于:
- (NSManagedObjectContext *)backgroundMOC:(NSManagedObjectContext *)mainMOC
{
NSManagedObjectContext * threadManagedObjectContext = [[NSManagedObjectContext alloc]
initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[threadManagedObjectContext setParentContext:mainMOC];
[threadManagedObjectContext setMergePolicy:NSMergeByPropertyObjectTrumpMergePolicy];
return threadManagedObjectContext;
}
将self.model.context
作为mainMOC传递,使用self.model.context
和setMergePolicy
。
同样,我必须假设self.model saveWithErrorBlock
在技术上与:
[mainMOC performBlockAndWait:^{
if([mainMOC hasChanges]) {
NSError * error;
[mainMOC save:&error];
// handle error
}
}];
如果是这样,backgroundMOC
(您的private
引用)也应如此:
[backgroundMOC performBlockAndWait:^{
if([backgroundMOC hasChanges]) {
NSError * error;
[backgroundMOC save:&error];
// handle error
}
}];
换句话说,您希望确保backgroundMOC
和mainMOC
保存操作是通过performBlockAndWait
从各自的线程执行的。