Core Data: Low level retrieving of data
Core Data: Low level retrieving of data
- Subject: Core Data: Low level retrieving of data
- From: Ryan Homer <email@hidden>
- Date: Thu, 25 Jan 2007 09:07:56 -0500
- Resent-date: Thu, 25 Jan 2007 12:54:40 -0500
- Resent-from: Ryan Homer <email@hidden>
- Resent-message-id: <email@hidden>
- Resent-to: Apple Cocoa-Dev Mailing List <email@hidden>
Hi everyone,
I'm using Core Data with bindings to create a sort of address book
(for use in an app I'm currently working on). It works as it's
supposed to in that I can create new contacts and store/retrieve them
from persistent store. All this is done at a "high level" using IB,
bindings, etc.
Now, I need to be able to access this data elsewhere in my code and
do other things with it (other than add/create/modify records and
store/retrieve them). To do this, and I don't know if this is the
correct way, I'm accessing the store using the code below, having
followed Apple's Low Level Core Data Tutorial. Here's what happens:
if I create a new entity at the point illustrated in the code, then
when I try to fetch, my resulting array has two objects, the new one
I created and the one contact from the store (I printed out the
object in the console and I can see all the data). If I try to access
the data using my custom NSManagedObject, I can access the first
contact which was created in memory, but trying to access the data
from the second contact gives errors (uncaught exception was raised,
SIGTRAP).
If I do not create the new entity, then when I try to fetch, I'm not
able to retrieve any data from the store. I think part of the problem
(only part) is that I am not using a custom NSManagedObject in my
GUI, but when retrieving I'm trying to use the custom class in order
to use my accessors. However, this should not affect the retrieval of
the data because it's only after the fetch that I use an enumerator
and try to access the returned NSManagedObjects as my custom
NSManagedObject.
Do I need a custom NSManagedObject to read my fields (sorry, I don't
know what the correct term is but I mean my data such as name, email,
etc.)? I've tried without and I keep getting "Target does not respond
to this message selector".
// These implementations left out due to space limitations. I'm
pretty confident they are correct due to the behaviour described
above. Plus they are almost verbatim from the Apple CD Tutorials
- (NSString *)applicationSupportFolder;
- (NSManagedObjectModel *)managedObjectModel;
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator;
- (NSManagedObjectContext *) managedObjectContext;
/* This is the function I call to return all my contacts */
- (NSArray *)addressBook
{
NSManagedObjectContext *moc = [self managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:@"Contact" inManagedObjectContext:moc];
/* If I include the following line, it will create a new entity and
then when I fetch,
* the result is an array with two NSManagedObjects,
* the empty one and the one from the persistent store
*/
/*RHMSNAddressBookMO *ab = [[[RHMSNAddressBookMO alloc]
initWithEntity:entityDescription
insertIntoManagedObjectContext:moc] autorelease];*/
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if ((error != nil) || (array == nil)) {
NSLog(@"Error while fetching\n%@",
([error localizedDescription] != nil) ? [error
localizedDescription] : @"Unknown Error");
return nil;
}
NSLog(@"Fetch results:%@",array);
return array; //return [[array retain] autorelease];
}
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden