Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

misee

macrumors member
Original poster
Jul 4, 2010
65
0
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.
 
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.
 
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).
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.