• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How do you have a core data based app handle non core data based docs?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do you have a core data based app handle non core data based docs?


  • Subject: Re: How do you have a core data based app handle non core data based docs?
  • From: Scott Ellsworth <email@hidden>
  • Date: Thu, 1 Sep 2005 14:17:54 -0700


On Sep 1, 2005, at 1:47 PM, William E. Jens wrote:

Specifically, my app needs to handle a file format that wasn't rooted
within Core Data. Where do you intercept Core Data's handling of loading
a doc?

There are a lot of ways to handle this, but I believe I have one that is supported.


First off, assuming you are using the Core Data Document based application stationary, you already have three document types defined. (Chris Hanson yelled at me until I changed my extensions from xml, binary, and sql to secxml, secbinary, and secsql. I am passing along the yelling.)

Add a new document type. In my case, I added "sectab" for "Tab Delimited Sector"
Override initWithContentsOfURL
Look for your new data types
If you spot one
Select one of the core data types that this should become.
Load up the data model
If you do not
Call the appropriate parent


- (id)initWithContentsOfURL:(NSURL *)absoluteURL ofType:(NSString *) typeName error:(NSError **)outError{
if ([typeName isEqualToString:@"Tab Delimited Sector"]){
self = [super initWithType:@"SQL" error:outError];
if (self){
[self initInstanceVariables];
NSString * data = [NSString stringWithContentsOfURL:absoluteURL encoding:NSMacOSRomanStringEncoding error:outError];
if (data==nil){
NSLog(@"Error reading file at %@: %@", [absoluteURL path], outError);
return NO;
} else {
[self setFromString:data];
[self windowControllersRefreshEverything];
}
}
} else {
self = [super initWithContentsOfURL:absoluteURL ofType:typeName error:outError];
if (self) {
[self initInstanceVariables];
}
}
return self;
}


I'm thinking that I would like to automatically import the older
doc and save it as a core data version.

That is what I am doing, and it works very well.

I'd still need to be able to
export into the older doc format, but going forward I'd deal with core
data internally and only export as needed.

Corresponding code:
Add an implementation of writeableTypesForSaveOperation that adds the types you want to export as, and only return your new types on a Save To:


- (NSArray *)writableTypesForSaveOperation:(NSSaveOperationType) saveOperation{
NSArray * result = [super writableTypesForSaveOperation:saveOperation];
if (saveOperation==NSSaveToOperation){
// filter out all but good ones
NSPredicate * onlySector = [NSPredicate predicateWithFormat:@"SELF contains[c] 'sector'"];
result = [result filteredArrayUsingPredicate:onlySector];
}
return result;
}


Now add an implementation of writeToURL that asks Core Data to do the right thing for core data types, but writes out your export types.

- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName forSaveOperation:(NSSaveOperationType)saveOperation originalContentsURL:(NSURL *)absoluteOriginalContentsURL error: (NSError **)outError{
if (saveOperation==NSSaveToOperation && ([typeName isEqualToString:@"Tab Delimited Sector"])){
NSString * data = [self dataAsStringTabDelimited];
NSError *error = nil;
BOOL writeResult = [data writeToURL:absoluteURL atomically:YES encoding:NSMacOSRomanStringEncoding error:&error];
if (writeResult == NO){
NSLog(@"Failed to write file at %@, error %@", absoluteURL, error);
}
return writeResult;
} else {
return [super writeToURL:absoluteURL ofType:typeName forSaveOperation:saveOperation originalContentsURL:absoluteOriginalContentsURL error:outError];
}
}


I got this from some stuff Wolf Rentzsch's <http://rentzsch.com/> wrote - errors are mine.

Scott
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >How do you have a core data based app handle non core data based docs? (From: "William E. Jens" <email@hidden>)

  • Prev by Date: How do you have a core data based app handle non core data based docs?
  • Next by Date: Hide Button
  • Previous by thread: How do you have a core data based app handle non core data based docs?
  • Next by thread: Hide Button
  • Index(es):
    • Date
    • Thread