我在用魔法唱片库。很酷但我有麻烦了。我的第二个方法返回不正确的数据。第一和第二种方法必须返回相同数量的引号。findAllWithPredicate是否正常工作?
解锁和取引号
-(NSArray*) unlockAndFetchQuotes
{
CoreDataManager *instance = [CoreDataManager instance];
[instance unlockUnitNumber:1];
return [instance fetchQuotes];
}解锁单元号
-(void) unlockUnitNumber:(int) number
{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"identificator=%d", number];
NSArray *array =[CDInApp findAllWithPredicate:predicate];
if (array.count>0)
{
CDInApp *inApp = [array objectAtIndex:0];
inApp.isLockedValue = NO;
[[NSManagedObjectContext defaultContext] saveNestedContexts];
}
}取引号
-(NSArray*) fetchQuotes
{
int z=0;
NSArray *arr = [CDQuotes findAll];
for (CDQuotes * quotes in arr)
{
if (!quotes.inApp.isLockedValue)
{
z++;
}
}
NSLog(@"_____ unlocked quotes by first method %d", z);
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"inApp.isLocked != 1"];
int count = [CDQuotes countOfEntitiesWithPredicate:predicate];
NSLog(@"_____ unlocked quotes by second method %d", count);
NSArray *array = [CDQuotes findAllWithPredicate:predicate];
NSLog(@"total unlocked array %d", array.count);
return array;
}第一和第二种方法必须返回相同数量的引号。第二种方法不能正常工作。
我的输出
___ must be unlocked quotes 500
___ unlocked quotes by first method 500
___ unlocked quotes by second method 250
total unlocked array 250

UPDATE2
我发现了一个问题。首先,我做unlockAndFetchQuotes。我追踪这个函数,发现了一些奇怪的东西。Saving在获取引号之后,现在正在寻找解决方案来立即保存。
2012-12-21 18:13:32.992 CoreBug[6713:11603] _____ unlocked quotes by first method 8
2012-12-21 18:13:32.994 CoreBug[6713:11603] _____ unlocked quotes by second method 6
2012-12-21 18:13:32.996 CoreBug[6713:11603] -[NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback:](0x7464700) -> Saving <NSManagedObjectContext (0x7464700): *** DEFAULT ***> on *** MAIN THREAD ***
2012-12-21 18:13:32.997 CoreBug[6713:15603] -[NSManagedObjectContext(MagicalSaves) MR_saveWithErrorCallback:](0x8148ef0) -> Saving <NSManagedObjectContext (0x8148ef0): *** BACKGROUND SAVING (ROOT) ***> on *** BACKGROUND THREAD ***我打开了我的单元并取了引号。
发布于 2012-12-22 18:25:58
saveNestedContexts是异步方法。我需要用另一种方法
[localContext MR_saveNestedContextsErrorHandler:nil completion:^{
[self fetchQuotes];
}];发布于 2012-12-20 11:39:35
在循环中使用的是isLockedValue,而不是isLocked。据推测,isLockedValue不存在,并且始终是nil,因此循环中的所有项都将被计算在内。
相反,您的谓词似乎工作正常。
发布于 2012-12-20 19:32:41
您是否尝试过将谓词更改为
[NSPredicate predicateWithFormat:@"inApp.isLocked = NO"]https://stackoverflow.com/questions/13969861
复制相似问题