我正试图找出核心数据存在的问题。我理解错误意味着什么,我之前已经拥有它(并修复它),但我无法弄清楚为什么我现在得到它。
我的应用具有以下结构:
MPModel - > MPPlace和MPModel - > MPProject
其中MPModel是NSManagedObject的子类,MPPlace和MPProject是MPModel的子类。
数据模型与MPPlace和MPProject之间存在关系,其中MPPlace有许多MPProjects,而MPProject属于MPPlace。
当应用程序加载时,它会获取一些完美运行的MPPlace对象。当用户选择一个地点然后选择项目选项时,该应用程序会尝试检索项目列表。然而,这是app失败的地方,出现以下错误
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'Failed to process pending changes before save. The context is still dirty after 100
attempts. Typically this recursive dirtying is caused by a bad validation method,
willSave, or notification handler.'
每个MPModel都包含许多自定义方法,包括saveAllLocally,它只是将对象数组保存到持久性存储中(如果它们尚不存在),loadNestedResources从服务器获取与当前对象相关的新对象。我发现loadNestedResources方法发生了故障,但我无法弄清楚原因。
loadNestedResources方法如下所示:
- (void) loadNestedResourcesOfType:(Class)type atURL:(NSString *)resURL withBlock:(MPFindObjectsBlock)block
{
if (![type isSubclassOfClass:[MPModel class]]) {
block(nil, nil);
} else {
NSString *url = [NSString stringWithFormat:@"%@%@/%@%@", kMP_BASE_API_URL, [self.class baseURL], self.objID, [type baseURL]];
MPRequest *request = [[MPRequest alloc] initWithURL:url];
// Attempt to see if we already have this relation
NSString *relation = [[MPUtils stripClassName:type].lowercaseString stringByAppendingString:@"s"];
NSSet *relatedObjects = [self valueForKey:relation];
if (relatedObjects && relatedObjects.count > 0) {
// We have some objects so lets exclude these from our request
NSMutableArray *uuids = [NSMutableArray arrayWithCapacity:0];
for (MPModel *obj in relatedObjects) {
[uuids addObject:obj.uuid];
}
[request setParam:[uuids componentsJoinedByString:@";"] forKey:@"exclude_uuids"];
}
[MPUser signRequest:request];
[request setRequestMethod:@"POST"];
[request submit:^(MPResponse *resp, NSError *error) {
if (error) {
if (relatedObjects.count > 0) {
block([relatedObjects allObjects], nil);
} else {
block(nil, error);
}
} else {
// Combine all of our objects
NSMutableArray *allObjects = [[type createListWithResponse:resp] mutableCopy];
if (allObjects.count > 0) {
[allObjects addObjectsFromArray:[relatedObjects allObjects]];
// Make sure they're now all saved in the persistence store
NSArray *savedObjects = [MPModel saveAllLocally:allObjects forEntityName:NSStringFromClass(type)];
for (NSObject *obj in savedObjects) {
[obj setValue:self forKey:[MPUtils stripClassName:self.class].lowercaseString];
}
// Set them as our related objects for this relationship
[self setValue:[NSSet setWithArray:savedObjects] forKey:relation];
[MPModel saveAllLocally:savedObjects forEntityName:NSStringFromClass(type)];
block(allObjects, nil);
} else {
block([relatedObjects allObjects], nil);
}
}
}];
}
}
这些方法运行完美,直到第二次调用saveAllLocally,这是我得到错误的地方。 MPModel类还使用willSave方法,该方法具有以下内容:
- (void) willSave
{
NSDate *now = [NSDate date];
if (!self.updated_at) {
self.updated_at = now;
}
if (!self.created_at) {
self.created_at = now;
}
if (!self.uuid) {
self.uuid = [self createUUID];
}
if (!self.last_sync) {
self.last_sync = now;
}
if ([self isUpdated] && self.changedValues.count > 0) {
if (!self.attribute_updated_at) {
NSDictionary *attributes = [self.entity attributesByName];
NSMutableDictionary *dates = [NSMutableDictionary dictionaryWithCapacity:0];
for (NSString *attr in attributes.allKeys) {
[dates setObject:now forKey:attr];
}
[self setAttributeUpdatedDates:dates];
}
if (_updatedAtSet) {
_updatedAtSet = NO;
} else {
if ([self.changedValues.allKeys containsObject:@"last_sync"]) {
self.updated_at = [self.changedValues objectForKey:@"last_sync"];
} else {
self.updated_at = [NSDate date];
}
_updatedAtSet = YES;
NSDictionary *changed = [self changedValues];
NSMutableDictionary *dates = [[self attributeUpdatedDates] mutableCopy];
for (NSString *key in changed.allKeys) {
[dates setObject:now forKey:key];
}
[self setAttributeUpdatedDates:dates];
}
}
}
从我可以收集的内容来看,这应该没问题,因为如果_updatedAtSet变量设置为true,它不应该再设置任何值,但是它仍然在破坏,我无法找出原因!
请有人帮助我 感谢
答案 0 :(得分:0)
已经解决了。
我刚刚将_updatedAtSet = NO;
移动到didSave方法而不是它的位置,现在它正常工作。不管怎样,谢谢