我有一个人们正在使用的应用程序,有些用户遇到了错误我已经想到当我尝试从Coredata读取时它返回空值。
我已经做了一些阅读,发现coredata会给你一些棘手的东西,它只会在你需要的时候返回数据,从我读到的这个叫做断层或其他东西。有趣的是,这个错误不会发生在每个设备上。我认为它取决于内存或某些东西,它可以选择操作系统。
这是我用来读取coredata以及我的堆栈跟踪的代码。
- (NSMutableArray *)readManuf
{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO]; // added to fix fault, but not working.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Manuf" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *error = nil;
NSMutableArray *manufacturerDictionaryArray = [[NSMutableArray alloc] init];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Manuf *manuf in fetchedObjects) {
NSMutableDictionary *tempManufacturerDictionary = [[ NSMutableDictionary alloc] init];
[tempManufacturerDictionary setObject:manuf.hasModel forKey:@"HASMODEL"]; // error app calls over
[tempManufacturerDictionary setObject:manuf.isManufacturer forKey:@"ISMANUFACTURER"];
[tempManufacturerDictionary setObject:manuf.nicknames forKey:@"NICKNAMES"];
[tempManufacturerDictionary setObject:manuf.manufacturer forKey:@"MANUFACTURER"];
[tempManufacturerDictionary setObject:manuf.manufacturerID forKey:@"MANUFACTURERID"];
[manufacturerDictionaryArray addObject:tempManufacturerDictionary];
}
return manufacturerDictionaryArray;
}
继承我的堆栈跟踪。
<_NSCallStackArray 0x155f6790>(
0 ??? 0x0323d714 0x0 + 52680468,
1 CodeI 0x00059ba9 main + 0,
2 CoreFoundation 0x306488f3 <redacted> + 818,
3 CodeI 0x000d83eb -[CoreDataController readManuf] + 1134,
4 CodeI 0x0008c4cb -[EngineRequests GetManufacturers:CallingView:] + 806,
5 CodeI 0x00076ddf -[VehicleSearchViewController tableView:cellForRowAtIndexPath:] + 1278,
6 UIKit 0x3305c8f7 <redacted> + 410,
7 UIKit 0x33003c27 <redacted> + 1806,
8 UIKit 0x3300347d <redacted> + 184,
9 UIKit 0x32f29d59 <redacted> + 380,
10 QuartzCore 0x32ba762b <redacted> + 142,
11 QuartzCore 0x32ba2e3b <redacted> + 350,
12 QuartzCore 0x32ba2ccd <redacted> + 16,
13 QuartzCore 0x32ba26df <redacted> + 230,
14 QuartzCore 0x32ba24ef <redacted> + 314,
15 UIKit 0x32f2d3e1 <redacted> + 8232,
16 CoreFoundation 0x306d820b <redacted> + 14,
17 CoreFoundation 0x306d76db <redacted> + 206,
18 CoreFoundation 0x306d5ecf <redacted> + 622,
19 CoreFoundation 0x30640ebf CFRunLoopRunSpecific + 522,
20 CoreFoundation 0x30640ca3 CFRunLoopRunInMode + 106,
21 GraphicsServices 0x35546663 GSEventRunModal + 138,
22 UIKit 0x32f8d14d UIApplicationMain + 1136,
23 CodeI 0x00059c1d main + 116,
24 libdyld.dylib 0x3b39dab7 <redacted> + 2
)
因为我已经阅读了一篇文章,其中说过将对象放入像我这样的字典NSArray中会导致错误,因为当coredata尝试填充数组时,它需要在其原生对象中。
所以我已经将我的读取方法改为此。
- (NSArray *)readManuf
{
NSManagedObjectContext *context = [self managedObjectContext];
// Test listing all FailedBankInfos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Manuf" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
[fetchRequest setReturnsObjectsAsFaults:NO];
NSError *error = nil;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
return fetchedObjects;
}
返回本机对象的MutableArray。但是,当我尝试使用这个本机对象数组时,它仍然不会在任何时候填充。