我正在使用三个不同的NSFetchedResultsController从我的sqlite数据库中获取数据记录。在设备(iOS 3.1.3)上测试时,其中一个给我带来了困难。在模拟器(iOS 4.2,deploymentTarget 3.1.3)中运行它可以正常工作。
在设备上,fetch返回的对象既不是NSDictionaries,也不是NSDictionaries,也不是故障。
以下是fetchedResultsController的定义:
- (NSFetchedResultsController *)fetchedResultsControllerForLocID
{
if (_fetchedResultsControllerForLocID != nil)
return _fetchedResultsControllerForLocID;
NSSortDescriptor *sortDescriptor_00 = [[NSSortDescriptor alloc] initWithKey: kLocationsDatabaseLocationIDKeyPath
ascending: TRUE];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: sortDescriptor_00, nil];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName: kLocationsDatabaseAlternateNameEntityName
inManagedObjectContext: self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize: 80];
[fetchRequest setResultType: NSDictionaryResultType];
[fetchRequest setPropertiesToFetch: [NSArray arrayWithObject: kLocationsDatabaseLocationIDKeyPath]];
[fetchRequest setReturnsDistinctResults: TRUE];
[fetchRequest setReturnsObjectsAsFaults: FALSE];
[fetchRequest setSortDescriptors: sortDescriptors];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest
managedObjectContext: self.managedObjectContext
sectionNameKeyPath: nil
cacheName: kLocationDatabaseAltNameLocIDRequestCacheName];
self.fetchedResultsControllerForLocID = theFetchedResultsController;
[fetchRequest release];
[theFetchedResultsController release];
[sortDescriptor_00 release];
[sortDescriptors release];
return _fetchedResultsControllerForLocID;
}
这是实际的获取:
- (void) searchLocationsDBWithFRCFor: (NSString *) locationName
{
[NSFetchedResultsController deleteCacheWithName: kLocationDatabaseAltNameLocIDRequestCacheName];
NSString *normLocationName = [self normalizeString: locationName];
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"(%K >= %@) && (%K <= %@)", kLocationsDatabaseLocationSearchNameKeyPath, normLocationName, kLocationsDatabaseLocationSearchNameKeyPath, [self upperBoundSearchString: normLocationName]];
[[self.fetchedResultsControllerForLocID fetchRequest] setPredicate: searchPredicate];
NSError *error = nil;
if (![self.fetchedResultsControllerForLocID performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
NSMutableArray *tempArray = [[NSMutableArray alloc] init];
//this line craches on the device:
for(NSDictionary* tempIDDict in [self.fetchedResultsControllerForLocID fetchedObjects])
{
[tempArray addObject: [tempIDDict objectForKey: kLocationsDatabaseLocationIDKeyPath]];
}
searchPredicate = [NSPredicate predicateWithFormat:@"(%K IN %@)", kLocationsDatabaseLocationIDKeyPath, tempArray];
[tempArray release];
//some more fetching...
}
在模拟器中,我得到了这些SQL-Callbacks:
2011-01-22 20:31:33.660 App[24386:207] CoreData: sql: pragma cache_size=200
2011-01-22 20:31:33.661 App[24386:207] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2011-01-22 20:31:33.664 App[24386:207] CoreData: sql: SELECT DISTINCT t0.ZLOCATIONID FROM ZALTERNATENAME t0 WHERE ( t0.ZLOCATIONSEARCHNAME >= ? AND t0.ZLOCATIONSEARCHNAME <= ?) ORDER BY t0.ZLOCATIONID
2011-01-22 20:31:33.666 App[24386:207] CoreData: annotation: sql connection fetch time: 0.0015s
2011-01-22 20:31:33.666 App[24386:207] CoreData: annotation: total fetch execution time: 0.0024s for 6 rows.
然而,在设备上运行它,给我这个:
2011-01-22 20:39:07.318 App[241:207] CoreData: sql: pragma cache_size=1000
2011-01-22 20:39:07.324 App[241:207] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2011-01-22 20:39:07.348 App[241:207] CoreData: sql: SELECT 0, t0.Z_PK FROM ZALTERNATENAME t0 WHERE ( t0.ZLOCATIONSEARCHNAME >= ? AND t0.ZLOCATIONSEARCHNAME <= ?) ORDER BY t0.ZLOCATIONID
2011-01-22 20:39:07.386 App[241:207] CoreData: annotation: sql connection fetch time: 0.0374s
2011-01-22 20:39:07.388 App[241:207] CoreData: annotation: total fetch execution time: 0.0404s for 16 rows.
2011-01-22 20:39:07.400 App[241:207] CoreData: sql: SELECT DISTINCT t0.ZLOCATIONID FROM ZALTERNATENAME t0 WHERE t0.Z_PK IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ORDER BY t0.ZLOCATIONID LIMIT 80
2011-01-22 20:39:07.407 App[241:207] CoreData: annotation: sql connection fetch time: 0.0061s
2011-01-22 20:39:07.409 App[241:207] CoreData: annotation: total fetch execution time: 0.0085s for 6 rows.
也不错,如果没有出现错误:
2011-01-22 20:39:07.411 App[241:207] *** -[NSKnownKeysDictionary1 objectID]: unrecognized selector sent to instance 0x1e4f80
2011-01-22 20:39:07.414 App[241:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSKnownKeysDictionary1 objectID]: unrecognized selector sent to instance 0x1e4f80'
2011-01-22 20:39:07.417 App[241:207] Stack: (
861696817,
860329709,
861700631,
861203093,
861166272,
832412333,
832413791,
77245,
66451,
66643,
845720837,
861449139,
861447005,
861059891,
861060063,
834770799,
834765939,
12391,
12316
)
terminate called after throwing an instance of 'NSException'
Program received signal: “SIGABRT”.
为什么Core Data会在设备上运行时改变查询?
NSKnownKeysDictionary1究竟是什么?为什么尝试访问控制器的fetchedObjects会在设备上出现错误?
有什么想法吗?提前谢谢!
答案 0 :(得分:2)
删除以下行,看看是否能解决问题。 [fetchRequest setFetchBatchSize:80];
我有一个类似的问题,我的应用程序运行良好,但在使用SDK 4.2构建的iOS 3.x设备上崩溃。我在这里找到了解决方案: setFetchLimit and sectionNameKeyPath on iPhone OS4 (XCode 3.2.3) cause crash