NSUInteger numberOfChanges = moc.insertedObjects.count + moc.deletedObjects.count+moc.updatedObjects.count;
if (numberOfChanges ==0 )
{
NSAssert([moc hasChanges]==false,@"[moc hasChanges]==false");
return;
}
不知何故,断言失败了。我想知道为什么。所以什么都没有插入。什么都没有被删除。什么都没有更新。然而 [moc hasChanges] 是真的吗?这种情况很少发生。但是,它根本不应该发生。
如果人们想看,这是完整的代码。
+(void)commit {
[BGHPTools breakIfLock];
NSManagedObjectContext *moc = [self managedObjectContext];
NSArray * arUpdatedObjects = moc.updatedObjects.allObjects;
NSArray * arUpdatedObjectsID = [arUpdatedObjects convertByPeformingSelector:@selector(objectID)];
NSUInteger numberOfChanges = moc.insertedObjects.count + moc.deletedObjects.count+moc.updatedObjects.count;
if (numberOfChanges ==0 )
{
//NSAssert([moc hasChanges]==false,@"[moc hasChanges]==false");
return;
}
if (arUpdatedObjectsID.count) {
while (false);
}
[BGFetchClass vAddObjectIDCachesForArray:moc.insertedObjects.allObjects];
[BGFetchClass vDeleteObjectsForArray:moc.deletedObjects.allObjects];
/*if (numberOfChanges ==0 )
{
NSAssert([moc hasChanges]==false,@"[moc hasChanges]==false");
return;
}*/
//NSAssert([moc hasChanges],@"[moc hasChanges]==true");
__block NSError *error;
__block BOOL saveSuccesfully;
[moc performBlockAndWait:^{
@synchronized([BGFetchClass class])
{
saveSuccesfully = [moc save:&error];
if (!saveSuccesfully) {
CLog(@"Error in Saving %@", error);
}
else{
}
}
}];
if (![NSThread isMainThread]) {
if (arUpdatedObjectsID.count) { //When we're adding new objects, this won't be called. That is the only time commit is called while we are synching
[BGHPTools vDoForeGroundAndWait:^{
NSManagedObjectContext * moc =[BGMDCRManagedObjectContextThreadHandler managedObjectContext];
for (NSManagedObjectID * moi in arUpdatedObjectsID) {
NSManagedObject * mo = [moc existingObjectWithID:moi error:nil];
NSAssert(mo!=nil, @"Object can't possibly be nil");
[mo turnToFault];
}
}];
}
}
NSManagedObjectContext * parentMoc = [self managedObjectContextMainContext]; //Main parent is not nsmainqueueconcurency type. Hence, this is save
[parentMoc performBlockAndWait:^{
if (![parentMoc save:&error])
{
CLog(@"Error in Saving %@", error);// handle error
}
}];
NSAssert(error==nil, @"Error must be nill");
}