iphone - Choosing unique label for NSManagedObject in CoreData -


i'm searching better alternative deal problem.

in coredata model have nsmanagedobject called project. in subclass override accessor method (setter) label attribute. here check whether same label used. if is, add underscore , number label, e.g. "myproject" renamed "myproject_1". of course have check whether find label "myproject" or "myproject_"+number. regular expression.

nsstring *regexstring = [nsstring stringwithformat:@"%@_[0-9]+", value];  nspredicate *predicate = [nspredicate predicatewithformat:@"(label = %@) or (label matches %@)", value, regexstring]; [request setpredicate:predicate]; 

then check how many results fetched, lets 5, know next 1 hast called "myproject_6".

it works fine have noticed there little problem code: happens if have following labels:

myproject_1, myproject_2, myproject_3

and user decides call project myproject_55. search retrieve 4 elements , next project labeled myproject_5 instead of myproject_4. , it's worse, @ point, end having 2 myproject_55. know it's unlikely happen, can :).

any ideas better?

here's accessor method

#pragma mark - setter label  - (void)setlabel:(nsstring *)alabel {     if ([[self primitivevalueforkey:@"label"] isequaltostring:alabel])      {         return;     }      nsmutablearray *objects = [self fetchobjectswithvalueequalto:alabel];     nsuinteger objectscount = [objects count];      alabel = objectscount > 0 ? [nsstring stringwithformat:@"%@_%d",alabel, objectscount] : alabel;      [self willchangevalueforkey:@"label"];     [self setprimitivevalue:alabel forkey:@"label"];     [self didchangevalueforkey:@"label"]; } 

its little expensive simplest way out of dilemma once have new label decision "mylabel_4" recheck if label exists in store.

rinse , repeat until have unique label. core data efficient isnt going matter in userland case.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -