I've begun having a strange issue with a Core Data app I'm writing. I'm trying to perform an NSFetchRequest with a simple predicate that is returning an array with the number of elements I expect, but each of the elements is of type invalid.
The predicate for this request is a string comparison. I've verified that the Core Data entity has this particular field set with sqlite3 on the command line, and I've confirmed that the string value I'm passing into the predicate is correct.
Just to be safe I even removed the predicate entirely. When I do that I get back an array which has a total number of elements matching the entire data set for the Core Data entity I'm fetching, but again every element has a type of invalid.
After setting a breakpoint and inspecting the invalid element I get this in the console:
Printing description of [0]:
(<invalid>) [0] = <error: expected ']'
error: 1 errors parsing expression
>
In another part of the application I'm doing a similar request (no predicate, just fetching the entire set) against this same entity and it's working fine. I'm stumped on how to continue troubleshooting this, and can't seem to find any information on Google as to why the fetch request is returning these results.
Here is the relevant code:
//appdelegate
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
_managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSMainQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator: coordinator];
}
return _managedObjectContext;
}
//ClientDetailModel.m
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSError *error;
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Clients" inManagedObjectContext: context];
NSFetchRequest *clientReq = [[NSFetchRequest alloc] init];
[clientReq setEntity: ed];
NSString *predicateString = [NSString stringWithFormat: @"%@\"%@\"", @"uuid == ", clientUuid];
[clientReq setPredicate: [NSPredicate predicateWithFormat: predicateString]];
NSArray *results = [context executeFetchRequest: clientReq error: &error];
And the Clients core data model:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Orders;
@interface Clients : NSManagedObject
@property (nonatomic, retain) NSNumber * account;
@property (nonatomic, retain) NSString * address;
@property (nonatomic, retain) NSString * city;
@property (nonatomic, retain) NSString * company;
@property (nonatomic, retain) NSString * contact;
@property (nonatomic, retain) NSString * email;
@property (nonatomic, retain) NSNumber * email1on;
@property (nonatomic, retain) NSString * email2;
@property (nonatomic, retain) NSNumber * email2on;
@property (nonatomic, retain) NSString * fax;
@property (nonatomic, retain) NSDate * last_synced;
@property (nonatomic, retain) NSDate * last_updated;
@property (nonatomic, retain) NSString * phone;
@property (nonatomic, retain) NSNumber * portal_id;
@property (nonatomic, retain) NSString * postal_code;
@property (nonatomic, retain) NSString * province;
@property (nonatomic, retain) NSNumber * sync_failures;
@property (nonatomic, retain) NSString * uuid;
@property (nonatomic, retain) NSString * client_hash;
@property (nonatomic, retain) NSSet *client_orders;
@end
@interface Clients (CoreDataGeneratedAccessors)
- (void)addClient_ordersObject:(Orders *)value;
- (void)removeClient_ordersObject:(Orders *)value;
- (void)addClient_orders:(NSSet *)values;
- (void)removeClient_orders:(NSSet *)values;
@end
As it turns out invalid is a perfectly valid type for an object returned from Core Data. In fact just casting the object of type invalid to a NSManagedObject solved the "problem".
NSManagedObject *client = [results objectAtIndex: 0];
I'm pretty confused as to why the data type would show as invalid when everything is apparently a-ok, but I suppose I should have just been "thinking differently".