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?