iphone - NSManagedObject hierarchy import and export -


i'm on run make nsmangedobjectclass profile im-/exportable.
try this way
exporting works correctly if write relationships in nsarrays because nsset doesn't have writetofile implemented.

- (void) exportprofile:(profile *)profile topath:(nsstring *)path{ //profile nsmutabledictionary *profiledict = [[self.selectedprofile dictionarywithvaluesforkeys:[[[self.selectedprofile entity] attributesbyname] allkeys]] mutablecopy]; nsmutablearray *views = [nsmutablearray array];  //views (view *view in selectedprofile.views) {     nsmutabledictionary *viewdict = [[view dictionarywithvaluesforkeys:[[[view entity] attributesbyname] allkeys]] mutablecopy];     nsmutablearray *controls = [nsmutablearray array];          //much more for-loops     [viewdict setobject:controls forkey:@"controls"];     [views addobject:viewdict]; }  [profiledict setobject:views forkey:@"views"];  if([profiledict writetofile:[path stringbystandardizingpath] atomically:yes])      nslog(@"saved"); else     nslog(@"not saved"); [profiledict release]; } 

but if want import on other side

- (profile*) importprofilefrompath:(nsstring *)path{ nsmanagedobjectcontext *context = [self.fetchedresultscontroller managedobjectcontext]; profile *newprofile = [nsentitydescription insertnewobjectforentityforname:@"profile" inmanagedobjectcontext:context];  nsmutabledictionary *profiledict = [nsmutabledictionary dictionarywithcontentsoffile:[path stringbystandardizingpath]]; [newprofile setvaluesforkeyswithdictionary:profiledict]; } 

i exception, doesn't confuse me cause profile expects nsset , no nsarray.
[__nscfarray intersectsset:]: unrecognized selector sent instance 0x4e704c0 *** terminating app due uncaught exception 'nsinvalidargumentexception',
reason: '-[__nscfarray intersectsset:]: unrecognized selector sent instance 0x4e704c0'

i've got 2 problems:

  • on 1 side, can't write nsset file.
  • on other side profile class expecting nsset.

so tried create category of nsset implements writetofile

@implementation nsset(persistence)  - (bool)writetofile:(nsstring*)path atomically:(bool)flag{     nsmutablearray *temp = [nsmutablearray arraywithcapacity:self.count];     for(id element in self)         [temp addobject:element];     return [temp writetofile:path atomically:flag]; }  + (id)setwithcontentsoffile:(nsstring *)apath{     return [nsset setwitharray:[nsarray arraywithcontentsoffile:apath]]; } @end 

but functions aren't called.

is there other way write nsset or tell setvaluesforkeyswithdictionary key "views" nsarray?

or easy way im-/export managedobjects?

got problems nested nsdictonarys, said goodbye dynamic way. here complete solution others
viewcontroller call im/export functions

- (void) exportprofile:(profile *)profile topath:(nsstring *)path{     //call nsmanagedobject function export     nsdictionary *profiledict = [self.selectedprofile dictionaryforexport];      if([profiledict writetofile:[path stringbystandardizingpath] atomically:yes])          nslog(@"saved");     else         nslog(@"not saved"); }  - (void) importprofilefrompath:(nsstring *)path{     nsmanagedobjectcontext *context = [self.fetchedresultscontroller managedobjectcontext];     profile *newprofile = [nsentitydescription insertnewobjectforentityforname:@"profile" inmanagedobjectcontext:context];      //load dictonary file     nsmutabledictionary *profiledict = [nsmutabledictionary dictionarywithcontentsoffile:[path stringbystandardizingpath]];     //call nsmanagedobjects import function     [newprofile importwithdictonary:profiledict context:context];      nserror *error = nil;     if (![context save:&error]) {          nslog(@"unresolved error %@, %@", error, [error userinfo]);         abort();     } } 

nsmanagedobject functions got hierarchy put them in each of nsmanagedobjects

- (void) importwithdictonary:(nsdictionary*)dict context:(nsmanagedobjectcontext*)context{     self.name = [dict objectforkey:@"name"];      //relationship     (nsdictionary *view in [dict objectforkey:@"views"]) {         view *tempview = [nsentitydescription insertnewobjectforentityforname:@"view" inmanagedobjectcontext:context];         [tempview importwithdictonary:view context:context];         tempview.profile = self;         [self addviewsobject:tempview];     } }  - (nsdictionary*) dictionaryforexport{      //propertys     nsmutabledictionary *dict = [[[self dictionarywithvaluesforkeys:[[[self entity] attributesbyname] allkeys]] mutablecopy] autorelease];     nsurl *objectid = [[self objectid] urirepresentation];     [dict setobject: [objectid absolutestring] forkey:@"objectid"];     nsmutablearray *views = [nsmutablearray array];      //relationship     (view *view in self.views) {         [views addobject:[view dictionaryforexport]];     }     [dict setobject:views forkey:@"views"];     return dict; } 

not beautiful solution works :)
, have still figure out how avoid duplicates in n:m relationship

thanks


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -