gpt4 book ai didi

objective-c - 如果 managedobjectcontext 发生变化,那么必须删除、更新或添加某些内容,这总是正确的吗?

转载 作者:行者123 更新时间:2023-12-04 04:34:31 24 4
gpt4 key购买 nike

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");
}

最佳答案

有一种记录在案的情况可以让您出现这种行为。
来自 NSManagedObjectContext文档:

NSManagedObjectContext deletedObjects

Discussion
The returned set does not necessarily include all the objects that have been deleted (using deleteObject:)—if an object has been inserted and deleted without an intervening save operation, it is not included in the set.



会不会是你的情况?

关于objective-c - 如果 managedobjectcontext 发生变化,那么必须删除、更新或添加某些内容,这总是正确的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19976028/

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