core data - How to insert NSManagedObject in other NSManagedObjectContext? -
i getting data server , converts them array of nsmanagedobject objects. array used display table.
how insert first element array peoples in persistent store?
- (void)viewdidload { [self loaddata]; [self insertfirstpeople]; } - (nsmanagedobjectcontext *)managedobjectcontext { if(!_managedobjectcontext) _managedobjectcontext = [nsmanagedobjectcontext mr_context]; return _managedobjectcontext; } - (void)loaddata { ... network request ... peoples = [nsmutablearray array]; (nsdictionary *item in items) { people *people = [podcast mr_createincontext:self.managedobjectcontext]; people.name = [item valueforkeypath:@"im:name.label"]; [peoples addobject:people]; } } -(void)insertfirstpeople { people *people = peoples[0]; nsmanagedobjectcontext *moc = [nsmanagedobjectcontext mr_defaultcontext]; [moc insertobject:people] [moc mr_savetopersistentstoreandwait]; }
error:
an nsmanagedobject may in (or observed by) single nsmanagedobjectcontext.
i myself have found solution problem.
-(void)insertfirstpeople { people *people = peoples[0]; coredatahelper *helper = [[coredatahelper alloc] init]; nsmanagerobjectcontext *context = [nsmanagedobjectcontext mr_defaultcontext]; [helper saveobject:people tocontext:context]; [context mr_saveonlyselfandwait]; }
coredatahelper.h
#import <foundation/foundation.h> @interface coredatahelper : nsobject { nsmutabledictionary* _lookup; } @property(nonatomic, retain) nsmutabledictionary *lookup; -(void)savefrom:(nsmanagedobjectcontext *)current to:(nsmanagedobjectcontext *)parent; - (nsmanagedobject *)saveobject:(nsmanagedobject *)object tocontext:(nsmanagedobjectcontext *)moc; - (nsmanagedobject*)copyobject:(nsmanagedobject*)object tocontext:(nsmanagedobjectcontext*)moc parent:(nsstring*)parententity; @end
coredatahelper.m
#import "coredatahelper.h" @implementation coredatahelper @synthesize lookup = _lookup; -(void)savefrom:(nsmanagedobjectcontext *)current to:(nsmanagedobjectcontext *)parent { nsnotificationcenter *dnc = [nsnotificationcenter defaultcenter]; [dnc addobserverforname:nsmanagedobjectcontextdidsavenotification object:current queue:nil usingblock:^(nsnotification *notification) { [parent mergechangesfromcontextdidsavenotification:notification]; }]; nserror *error; if (![current save:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); } [dnc removeobserver:self name:nsmanagedobjectcontextdidsavenotification object:current]; } - (nsmanagedobject *)saveobject:(nsmanagedobject *)object tocontext:(nsmanagedobjectcontext *)moc { nsundomanager *docundomgr = [moc undomanager]; [docundomgr beginundogrouping]; nsmanagedobject *object2 = [self copyobject:object tocontext:moc parent:nil]; [moc processpendingchanges]; [docundomgr endundogrouping]; return object2; } - (nsmanagedobject *)copyobject:(nsmanagedobject *)object tocontext:(nsmanagedobjectcontext *)moc parent:(nsstring *)parententity; { nserror *error = nil; nsstring *entityname = [[object entity] name]; nsmanagedobject *newobject = nil; if ([moc objectregisteredforid:object.objectid]) newobject = [moc objectwithid:object.objectid]; else newobject = [nsentitydescription insertnewobjectforentityforname:entityname inmanagedobjectcontext:moc]; if (![moc save:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); } [[self lookup] setobject:newobject forkey:[object objectid]]; nsarray *attkeys = [[[object entity] attributesbyname] allkeys]; nsdictionary *attributes = [object dictionarywithvaluesforkeys:attkeys]; [newobject setvaluesforkeyswithdictionary:attributes]; id olddestobject = nil; id temp = nil; nsdictionary *relationships = [[object entity] relationshipsbyname]; (nsstring *key in [relationships allkeys]) { nsrelationshipdescription *desc = [relationships valueforkey:key]; nsstring *destentityname = [[desc destinationentity] name]; if ([destentityname isequaltostring:parententity]) continue; if ([desc istomany]) { nsmutableset *newdestset = [nsmutableset set]; (olddestobject in [object valueforkey:key]) { temp = [[self lookup] objectforkey:[olddestobject objectid]]; if (!temp) { temp = [self copyobject:olddestobject tocontext:moc parent:entityname]; } [newdestset addobject:temp]; } [newobject setvalue:newdestset forkey:key]; } else { olddestobject = [object valueforkey:key]; if (!olddestobject) continue; temp = [[self lookup] objectforkey:[olddestobject objectid]]; if (!temp && ![destentityname isequaltostring:parententity]) { temp = [self copyobject:olddestobject tocontext:moc parent:entityname]; } [newobject setvalue:temp forkey:key]; } } return newobject; } @end
Comments
Post a Comment