简体   繁体   中英

iPhone's Core Data crashes on fetch request

I'm using the following code to grab a few objects from SQLite store (which is a prepared SQLite db file, generated with Core Data on desktop):

        NSFetchRequest * request = [[NSFetchRequest alloc] init];
        [request setEntity: wordEntityDescription];
        [request setPredicate: [NSPredicate predicateWithFormat: @"word = %@", searchText]];

        NSError * error = [[NSError alloc] init];
        NSArray * results = [[dao managedObjectContext] executeFetchRequest: request error: &error];

Eveyrthing seems to be setup properly, but executeFetchRequest:error: fails deeply inside Core Data (on NSSQLCore _newRowsForFetchPlan:selectedBy:withArgument) producing 256 error to the outside code.

The only kink I had setting up managedObjectContext is I had to specify NSIgnorePersistentStoreVersioningOption option to addPersistentStoreWithType as it was constantly producing 134100 error (and yes, I'm sure my models are just identical: I re-used the model from the project that produced the SQL file).

Any ideas?

PS Don't mind code style, it's just a scratch pad. And, of course, feel free to request any additional info. It would be really great if someone could help.

Update 1 Alex Reynolds, thanks for willingness to help :) The code (hope that's what you wanted to see):

NSEntityDescription * wordEntityDescription; //that's the declaration (Captain Obviousity :)
wordEntityDescription = [NSEntityDescription entityForName: @"Word" inManagedObjectContext: ctx];

As for predicate – never mind. I was removing the predicate at all (to just grab all records) and this didn't make any differences.

Again, the same code works just fine in the desktop application, and that drives me crazy (of course, I would need to add some memory management stuff, but it at least should produce nearly the same behavior, shouldn't it?)

Can you add code to show how wordEntityDescription is defined?

Also, I think you want:

NSError *error = nil;

You may want to switch the equals symbol to like and use tick marks around the searchText field:

[request setPredicate: [NSPredicate predicateWithFormat: @"word like '%@'", searchText]];

NSPredicate objects are not put together like SQL, unfortunately. Check out Apple's NSPredicate programming guide for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM