Core Data Problem

Discussion in 'Mac Programming' started by Soulstorm, May 11, 2011.

  1. Soulstorm macrumors 68000


    Feb 1, 2005
    I made an application without Core Data, and I decided to implement Core Data afterwards.

    I created the entities inside the model, and created the files for these entities.

    However, whenever I call any property for these properties, I get a runtime error saying that the selector could not be recognized.

    This is the source for the managed object:

    @class CacheDB;
    @interface CachedFile : NSManagedObject {
    @property (nonatomic, retain) NSString * fileID;
    @property (nonatomic, retain) NSString * filePath;
    @property (nonatomic, retain) NSString * url;
    @property (nonatomic, retain) NSDate * date;
    @property (nonatomic, retain) CacheDB * database;
    @implementation CachedFile
    @dynamic fileID;
    @dynamic filePath;
    @dynamic url;
    @dynamic date;
    @dynamic database;
    and this is the creation code:
    CachedFile *newCachedFile = (CachedFile *)[NSEntityDescription entityForName:@"CachedFile" inManagedObjectContext:context];
    newCachedFile.url = downloader.url; //program crashes here...
    Any ideas of what I might have missed or what I may be doing wrong?

    Attached Files:

  2. jiminaus macrumors 65816


    Dec 16, 2010
    The code above isn't right. Sending entityForName:inManagedObjectContext: doesn't get you a managed object. It gets you an NSEntityDescription object. This object just contains the metadata directly from the model. It analogous to a class. You then use this to init an actually managed object.

    Try this code instead.

    NSEntityDescription *cachedFileEntityDescription =
       [NSEntityDescription entityForName:@"CachedFile"
    CachedFile *newCachedFile = 
       [[CachedFile alloc] 
    newCachedFile.url = downloader.url;
  3. Soulstorm thread starter macrumors 68000


    Feb 1, 2005
    I will never EVER post a question in the forums after 12 hours of work again. Thank you for the answer. I also have another question, more complex, but I will post it after I get some sleep :) . Sorry for wasting your time.
  4. Soulstorm thread starter macrumors 68000


    Feb 1, 2005
    I have another question:

    I managed to run the application using the code you suggested and it runs ok the first time, but the second time it opens (and uses the already created files) it doesn't work. Here is my CacheDB object:

    @class CachedFile;
    @interface CacheDB : NSManagedObject {
    @property (nonatomic, retain) NSDate * dateModified;
    @property (nonatomic, retain) NSSet* cachedFiles;
    - (void)addCachedFilesObject:(CachedFile *)value;
    - (void)removeCachedFilesObject:(CachedFile *)value;
    - (void)addCachedFiles:(NSSet *)value;
    - (void)removeCachedFiles:(NSSet *)value;
    @implementation CacheDB
    @dynamic dateModified;
    @dynamic cachedFiles;
    - (void)addCachedFilesObject:(CachedFile *)value {    
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"cachedFiles"] addObject:value];
        [self didChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
        [changedObjects release];
    - (void)removeCachedFilesObject:(CachedFile *)value {
        NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
        [self willChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
        [[self primitiveValueForKey:@"cachedFiles"] removeObject:value];
        [self didChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
        [changedObjects release];
    - (void)addCachedFiles:(NSSet *)value {    
        [self willChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"cachedFiles"] unionSet:value];
        [self didChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    - (void)removeCachedFiles:(NSSet *)value {
        [self willChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
        [[self primitiveValueForKey:@"cachedFiles"] minusSet:value];
        [self didChangeValueForKey:@"cachedFiles" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    Here is some code that is supposed to return an existing CacheDB object and if there is none, create it:

    - (CacheDB *)applicationDatabase
    	if (applicationDatabase == nil) {
    		NSLog(@"getting database..");
    		NSManagedObjectContext *appContext = [[SFGlobals sharedSFGlobals]applicationManagedObjectContext];
    		NSEntityDescription *desc = [NSEntityDescription entityForName:@"CacheDB" inManagedObjectContext:appContext];
    		NSFetchRequest *request = [[NSFetchRequest alloc]init];
    		[request setEntity:desc];
    		NSArray *fetchResults = [appContext executeFetchRequest:request error:nil];
    		[request release];
    		if ([fetchResults count] <= 0) {
    			NSLog(@"There is no database yet. Creating one now...");
    			//perhaps this is a memory leak or a bug?
    			CacheDB *newDatabase = (CacheDB *)[NSEntityDescription insertNewObjectForEntityForName:@"CacheDB" inManagedObjectContext:appContext];
    			newDatabase.dateModified = [NSDate date];
    			applicationDatabase = newDatabase;
    			NSLog(@"found database. inserting into variable and returning it...");
    			applicationDatabase = (CacheDB *)[fetchResults objectAtIndex:0];
    	NSLog(@"database contains %i items", [applicationDatabase.cachedFiles count]);
    	return applicationDatabase;
    The application runs ok and adds files using this code:

    - (void)createDataForURL:(NSString *)url withData:(NSData *)data
    	NSLog(@"creating data for url: %@", url);
    	if ( ![self itemAlreadyExistsForURL:url] ) {
    		NSLog(@"creating item...");
    		CachedFile *newCachedFile = (CachedFile *)[NSEntityDescription insertNewObjectForEntityForName:@"CachedFile" inManagedObjectContext:[[SFGlobals sharedSFGlobals]applicationManagedObjectContext]];
    		newCachedFile.url = url; = [NSDate date];
    		newCachedFile.fileID = [self generateFileID];
    		newCachedFile.filePath = [[self applicationDatabaseFolder]stringByAppendingPathComponent:newCachedFile.fileID];
    		[self addCachedtoDatabase:newCachedFile];
    		[data writeToFile:newCachedFile.filePath atomically:NO];
    		NSLog(@"item for %@ already exists. Nothing done", url);
    When I start the program second time, it crashes in the following line:
    NSLog(@"database contains %i items", [applicationDatabase.cachedFiles count]);

    and it complains about an unrecognized selector sent to NSDictionary.

    Any ideas?
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    Post the stack trace.

    Post the complete error message for the unrecognized selector.

    With or without garbage collection?

    If without, have you done a Build and Analyze, and have you run under Instruments with zombies enabled?

Share This Page