I’m fetching some objects out of a data store but the results aren’t what I’m expecting. I’m new to CoreData but I’m fairly certain this should work. What am I missing?
Note that User is a valid managed object and that I include its header file in this code, and that UserID is a valid property of that class.
NSFetchRequest *requestLocal = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"User" inManagedObjectContext:messageManagedObjectContext];
[requestLocal setEntity:entity];
// Set the predicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY UserID IN %@", userList];
[requestLocal setPredicate:predicate];
// Set the sorting
... sorting details removed but exist and are fine ...
// Request the data
NSArray *fetchResults = [messageManagedObjectContext executeFetchRequest:requestLocal error:&error];
[requestLocal release];
for (int i; i < [fetchResults count]; i++) {
[fetchResults objectAtIndex:i].UserID = ...<----HERE
}
Isn’t fetchResults an array of User objects? Wouldn’t [fetchResults objectAtIndex:i] be a User object? Why do I get an error when building that “request for member ‘UserID’ in something not a structure or union“?
Sorry if this is a basic error, I’m clearly missing some basic concept. I’ve done a ton of searching and it seems like it should be right. (I also tried fast enumeration but it complained that fetchResults items weren’t valid Objective C objects, effectively the same error, I think.)
Update:
(from comment below)
My goal is to update the object, calling saveAction after changing it.
Does the KVC method still refer to the actual object? I tried fast enumeration with:
for (User thisUser in fetchResults) {
… but it didn’t like that.
I used the more generic version:
(id thisUser in fetchResults)
…but it won’t let me set
[thisUser valueForKey:@"FirstName"] = anything
… insisting that there’s no Lvalue.
Will:
[[thisUser valueForKey:@"FirstName"] stringWithString:@"Bob"]
… do the trick or is there a better way? Sorry, I know it’s nearly a new question, but I still don’t get what is in the fetchResults array.
Your
fetchedResultsvariable contains a NSArray object. However, a NSArray can hold any arbitrary group of objects. Unlike a standard C array, there is no requirement that the NSArray objects all be of a single class.The dot notation you are using here:
… while a legal syntax, nevertheless confuses the compiler because the compiler has no idea what class of object is returned by
[fetchResults objectAtIndex:i]. Without knowing the class it has no idea what the heckUserIDis. Hence the error"request for member 'UserID' in something not a structure or union". At the very least you have to cast the return of[fetchResults objectAtIndex:i]to some class so that the complier has a clue as to what ‘UserID’ is.However, you simply shouldn’t use this construction even though it legal because it is dangerous. See below for the best practice form.
Understanding NSManagedObject and its subclasses can be tricky because NSManagedObject itself uses a trick called
associative storagewhich allows any generic NSManagedObject instances to store any property of any entity defined in any model. This can confuse novices because there are multiple ways to refer to the same entities, instances and properties. Sometimes the examples use generic NSMangedObjects andsetValue:forKey:/valueForKey:and other times they useobjectInstance.propertyName.Associative storage works like a dictionary attached to every instance of the NSManagedObject class. When you insert a generic NSManagedObject like this:
… you get an instance of the NSManageObject class whose associative storage keys are set to the properties of the
Userentity as defined in your data model. You can then set and retrieve the values using key-value coding (which has the same syntax as dictionaries) thusly:Associative storage allows you represent any complex data model in code without having to write any custom NSManagedObject subclasses. (In Cocoa, it allows you to use bindings which let you create entire programs without writing any data management code at all.)
However, the generic NSManagedObject class is little better than a glorified dictionary whose saving and reading is handled automatically. If you need data objects with customized behaviors you need to explicitly define a NSManagedObject subclass. If you let Xcode generate the class from the entity in the data model you end up with a source file something like:
Now, you are no longer limited by to the key-value syntax of associative storage. You can use the dot syntax because the complier has a class to refer to:
With all this in mind, the proper forms of reference to the
fetchedResultsarray become clear. Suppose you want to set all userID properties to a single default value. If you use the generic NSManagedObject class you use:If you use a dedicated subclass you would use:
(Note: you can always use the generic form for all NSManagedObject subclasses as well.)