iphone - Core Data Wont Persist Property Updates to Database -


i'm working subclass of nsmanagedobject. actually, inherits class inherits class inherits nsmanagedobject (that shouldn't problem, right?).

the problem

after make changes properties of object, object remembers changes lifetime, changes never saved database.

how know this?

i know because:

  • when restart app, changes i've made lost.
  • telling context refresh object – after i've made changes object , told context save – sets object's values original state before made changes.
  • when running app in simulator, can @ sqlite database file in finder, , it's modified date isn't updated when attempt save context.

nothing being written database!

context

i'm using auto-generated delegate methods create store coordinator , context. i'm passing context view controllers in init methods, recommended in docs. store sqlite.

i able insert objects database , read them. can make property changes newly inserted object , save successfully. don't seem able update object properties when object pulled out of database.

the object fetched store via relationship object. after making changes properties, call context's save method. however, before doing so, call object's isupdated method , context's haschanges method, , both return false. shouldn't return true since i've made changes object's properties haven't saved context?

more

if call object's committedchanges method before saving context, however, passing in names of properties i've changed, correct values of properties. i'm not sure means. have thought means object's new property values have been saved, not saved.

i know result objects registered context. if call

[[result managedobjectcontext] refreshobject:result mergechanges:yes]; 

the object reverts original property values. means context there , same context record fetched. , means new property values never written tot database.

some code

here's code i'm poking around of these things. there other places in code i'm making property changes, changes never saved.

- (ibaction)statuscontrolchanged:(uisegmentedcontrol *)control { wcaassessmentresult *result = [self currentresult];      /* printing existing property values */     if (![result.complete boolvalue]) nslog(@"result in progress!");     else if ([result.passed boolvalue]) nslog(@"result passed!");     else nslog(@"result not passed!");      /* changing property values */     switch (control.selectedsegmentindex) {         case 0:             nslog(@"setting incomplete");             result.complete = [nsnumber numberwithbool:no];             break;         case 1:             nslog(@"setting passed");             result.passed = [nsnumber numberwithbool:yes];             result.complete = [nsnumber numberwithbool:yes];             break;         case 2:             nslog(@"setting failed");             result.passed = [nsnumber numberwithbool:no];             result.complete = [nsnumber numberwithbool:yes];             break;         default:             break;     }      /* method returns empty dictionary */     nslog(@"%@", [result changedvalues]);      /* method returns values set */     nslog(@"%@", [result committedvaluesforkeys:[nsarray arraywithobjects:@"complete", @"passed", nil]]);      /* isupdated returns false */     if (![result isupdated]) {         nslog(@"result not updated?! wtf!?!?");     }      /* haschanges returns false */     if (![[result managedobjectcontext] haschanges]) {         nslog(@"context has no changes!? wtf!?!?");     }      /* saving context produces no error */     nserror *error = nil;     if (![[result managedobjectcontext] save:&error]) {         nslog(@"save failed");         nslog(@"%@",[error description]);     } } 

a twist

if create new result object inserting new record context, can set object's properties , saved successfully. in above code, i'm fetching object member of to-many association object. clue?

i'm tearing hair out on this. hell going wrong here?

what's not problem

  • i've logged object's class, , indeed correct class
  • i've made sure managedobjectcontext i'm saving same object's context
  • i haven't made changes auto-generated setter/getter methods of managed object classes
  • i've tried using setvalue:forkey: method instead of object's properties
  • i've used -com.apple.coredata.sqldebug 1 argument log core data sql, , no sql logged when update , save object's properties

i not understand statement

wcaassessmentresult *result = [self currentresult]; 

indeed, if accessing to-many relationship object, should set, not object. anyway, without seeing code it's hard tell. problem experiencing may or may not lie there.

i rather expect in code following snippet access objects belonging to-many relationship. assume yourobject object use access wcaassessmentresult objects in to-many relationship, call results.

nsmutableset *resultobjects = [yourobject mutablesetvalueforkey:@"results"]; nspredicate *predicate = ... [resultobjects filterusingpredicate:predicate];  for(wcaassessmentresult *result in resultobjects){     // modify needed current result object  }  nserror *error = nil; if (![managedobjectcontext save:&error]) {         nslog(@"save failed");         nslog(@"%@",[error description]); } 

did verify managedobjectcontext using save object valid (not nil) ?


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 -