FetchedResultsController returning unknown object
I'm using three different NSFetchedResultsController to fetch data records from my sqlite database. One of them is giving me a hard time, when tested on a device (iOS 3.1.3). Running it in the simulator (iOS 4.2, deploymentTarget 3.1.3) works fine.
On the device the fetch is returning objects, which are neither NSDictionaries, as requested, nor faults.
Here is the definition of the 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;
}
Here is the actual fetch:
- (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...
}
In the simulator I get these 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.
However running it on the device, gives me this:
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.
Not too bad, if it wasn't followed by an error:
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”.
Why is Core Data altering the query, when running on the device?
What exactly is a NSKnownKeysDictionary1? And why does trying to access the fetchedObjects of the controller gives me an error on the device?
Any ideas? Thanks in advance!
Remove the following line and see if that solves the problem. [fetchRequest setFetchBatchSize: 80];
I had a similar issue where my app worked fine but crashed on iOS 3.x devices built with SDK 4.2. I found the solution here: setFetchLimit and sectionNameKeyPath on iPhone OS4 (XCode 3.2.3) cause crash
精彩评论