Thursday, 15 March 2012

ios - Updating a flag when an attribute changes -



ios - Updating a flag when an attribute changes -

as part of syncing solution, utilize sync status objects of class. whenever specific (not all) attributes of object change, want update status.

i considering 4 approaches:

manually setting status in code wherever alter needs synced. obvious, laborious , error-prone (i'll need remember add together sync status update time add together new functionality). track using core info notification (e.g. willsave or nsmanagedobjectcontextobjectsdidchangenotification). seems appropriate @ first glance - sign notifications in appdelegate , update status each time. possible examine changes , update sync status when attribute care updated? also, won't setting sync_status fires notification, leading me endless loop? how address this? custom setters on attributes care about. have had problem trying working before, , decided seek leave standard core info getters/setters alone. delve in if best fit. kvo. i've not used pattern before, might easiest sign notifications of attribute changes care , set flag there. this? need monitor every object of class, possible start observing attribute's kvo notifications in same object's awakefrominsert? i.e., whenever object created, have same object hear changes attributea , set it's own sync_status when fires?

which of these approaches serve me best? perhaps missing other ideas?

manually setting status code

probably bad idea, reason describe. you'll need in kinds of cases. might not developer on app. 1 day or else forget it. if don't, code on place centralized.

track using core info notification [...] also, won't setting sync_status fires notification, leading me endless loop?

it depends how it. listening nsmanagedobjectcontextdidsavenotification work, if utilize secondary nsmanagedobjectcontext. way can set sync flag, save changes, , avoid looping because you're saving on different context you're not observing.

using nsmanagedobjectcontextobjectsdidchangenotification work. posted when object properties changed save not in progress yet. inspect userinfo dictionary see if care has changed, , if so, set sync_status flag. setting flag trigger new notification, 1 don't care about, break loop. using separate context prevent looping here.

custom setters on attributes care about.

definitely workable, though depending on how many attributes care about, end lot of accessors update sync status. of 4 mention, 1 use.

a related simpler approach override willsave on managed object classes. called before save. implement to

look through [self changedvalues] attributes trigger syncing. set sync flag if find of them.

this way have 1 custom method per entity, no matter how many attributes end triggering sync flag.

kvo

should work, less intuitive working right custom setters.

when had this, set sync info outside of info store. i'd hear nsmanagedobjectcontextdidsavenotification, , in observer method would:

look through userinfo see had changed get nsmanagedobjectid each object need sync convert object ids nsstrings , add together list saved in separate file.

on successful sync i'd clear out object id list.

the thinking sync flag more metadata actual model data, kept out of model. if prefer maintain in model, i'd go overriding willsave.

ios objective-c cocoa core-data sync

No comments:

Post a Comment