Simple question. Could anyone tell me what’s wrong with this piece of code?
+ (NSManagedObject*) managedObjectWithServerId:(NSInteger)serverId inManagedObjectContext:(NSManagedObjectContext*)context{
//Execute request and get all the results
NSError * error = nil;
NSFetchRequest * request = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass(self)];
request.predicate = [NSPredicate predicateWithFormat:@"serverId == %@" argumentArray:[NSArray arrayWithObject:[NSNumber numberWithInteger:serverId]]];
NSArray * results = [context executeFetchRequest:request error:&error];
}
The results array is always an NSArray with 0 objects, no errors are given, nothing. Just a 0-count array.
And it’s not possible to return always 0 elements, I am exploring the Core Data database with a SQLite explorer, and there are elements with the serverId I am requesting, so there should be at least 1.
Am I doing the predicate wrong?
The above code is implemented in an NSManagedObject category, and called as shown:
[User managedObjectWithServerId:whatev inManagedObjectContext:context];
Where User is an entity in my model and subclass of NSManagedObject.
Problem fixed, it seems I was always re-creating the Database file because of an unexpected condition. I noticed because all the Primary Keys were always starting from 1.
CoreData seems to have some issues if you don’t delete the previous UIManagedDocument Database file before creating the new one.
With the condition fixed, I get no NSPersistentStoreCoordinator crash error, I can see all the SQL methods with debugging and my fetches, of course, works correctly.
Thanks to all the people who tried helping, and sorry for wasting your time 🙂