Core Data Problem

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

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    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:

    Code:
    @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;
    
    @end
    
    @implementation CachedFile
    @dynamic fileID;
    @dynamic filePath;
    @dynamic url;
    @dynamic date;
    @dynamic database;
    
    
    @end
    
    
    and this is the creation code:
    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. macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.

    Code:
    NSEntityDescription *cachedFileEntityDescription =
       [NSEntityDescription entityForName:@"CachedFile"
                   inManagedObjectContext:context];
    CachedFile *newCachedFile = 
       [[CachedFile alloc] 
            initWithEntity:cachedFileEntityDescription
            insertIntoManagedObjectContext:context];
    newCachedFile.url = downloader.url;
    
     
  3. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #3
    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. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    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:

    Code:
    @class CachedFile;
    
    @interface CacheDB : NSManagedObject {
    @private
    }
    @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;
    @end
    
    @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];
    }
    
    
    @end
    
    
    Here is some code that is supposed to return an existing CacheDB object and if there is none, create it:

    Code:
    - (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;
    			
    		}else{
    			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:

    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;
    		newCachedFile.date = [NSDate date];
    		newCachedFile.fileID = [self generateFileID];
    		newCachedFile.filePath = [[self applicationDatabaseFolder]stringByAppendingPathComponent:newCachedFile.fileID];
    		[self addCachedtoDatabase:newCachedFile];
    		[data writeToFile:newCachedFile.filePath atomically:NO];
    	}else{
    		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. macrumors 603

    Joined:
    Aug 9, 2009
    #5
    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