我正在使用 Magical Record 库。它很酷,但我有一些麻烦。我的第二种方法返回不正确的数据。第一种和第二种方法必须返回相同数量的报价。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
更新2
我发现了一个问题。首先我做unlockAndFetchQuotes。我跟踪这个函数,发现了一些奇怪的东西。获取报价后保存到 coredata。现在我正在寻找可以立即保存的解决方案。
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 ***
我解锁了我的单位并获取了报价。