Should I use Core Data

Discussion in 'Mac Programming' started by misee, Jan 21, 2011.

  1. misee macrumors member

    Joined:
    Jul 4, 2010
    #1
    I have an application that reads (and writes) SubRip subtitle files. I am now doing a complete rewrite of the code and am stuck with a design decision: Should I use Core Data or not.

    I understand that Core Data would bring me some benefits, like undo/redo support, which can be rather complex to implement.

    I've never used Core Data before (except for some playing around) and am not sure whether or not to go all Core Data on my app. Here my question: Should I
    • create my own custom atomic store,
    • use an in-memory-store, adding subtitles from an NSDocument subclass or
    • forget about Core Data entirely?

    My application is targeted for 10.5, so that is not an argument against creating a custom atomic store.

    I'd be glad for any advice.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I haven't done extensive work with Core Data, but I'd suggest giving it try. You can store data in memory instead of to file - see NSInMemoryStoreType.

    The SubRip file format looks very simple, so it'd be quite straightforward to convert to and from Core Data.
     
  3. Blakeasd macrumors 6502a

    Joined:
    Dec 29, 2009
    #3
    I would recommend Core Data. It makes everything a lot easier.
     
  4. misee, Jan 22, 2011
    Last edited: Jan 22, 2011

    misee thread starter macrumors member

    Joined:
    Jul 4, 2010
    #4
    Thanks for the replies. I guess I'll read through some CD documentation then.
    From what I've read until now, I figured that it would be best to use an in-memory store and add the items in the file programmatically. Now, from the NSPersistentDocument documentation, I got the impression that by having a subclass of NSPersistentDocument and overriding some methods as follows should do the trick:
    Code:
    - (NSString *)persistentStoreTypeForFileType:(NSString *)fileType {
        if (![self _isSubRipType:fileType])
            return [super persistentStoreTypeForFileType:fileType];
        
        return NSInMemoryStoreType;
    }
    
    - (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError {
        if (![self _isSubRipType:typeName])
            return [super readFromURL:absoluteURL ofType:typeName error:outError];
        
        return [self readFromData:[NSData dataWithContentsOfURL:absoluteURL] ofType:typeName error:outError];
    }
    
    - (BOOL)revertToContentsOfURL:(NSURL *)inAbsoluteURL ofType:(NSString *)inTypeName error:(NSError **)outError {
        if (![self _isSubRipType:inTypeName])
            return [super revertToContentsOfURL:inAbsoluteURL ofType:inTypeName error:outError];
        
        /*
         * Implement revert operation here.
         */
        return NO;
    }
    
    - (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:(NSError **)outError {
        if (![self _isSubRipType:typeName])
            return [super writeToURL:absoluteURL ofType:typeName forSaveOperation:saveOperation originalContentsURL:absoluteOriginalContentsURL error:outError];
        
        NSData *data = [self dataOfType:typeName error:outError];
        return data != nil && [data writeToURL:absoluteURL atomically:YES];
    }
    However, in the Core Data documentation (see Custom store types), I read that if you use an in memory store, you need to register for NSManagedObjectContextDidSaveNotification notifications. I haven't run the above code yet, so I don't know to which extent it will work, but I don't quite see why I would have to register for the said notifications. Does anyone know more about this? Did I miss something?
    With the code above, it would be easy to add a more formats or save as a file type supported by CD (if I ever feel the need to add such functionality).
     

Share This Page