objective c - iOS Realm cascade delete -
my models are: first model - job: idx, title, description
second model - specialization: idx, title, rlmarray<job> jobs
what should if want delete job , i'd delete specs related job. in advance. code is:
[self.storage beginwritetransaction]; rlmresults *specs = [mbspecialization objectsinrealm:self.storage where:@"%@ in jobs", job]; (mbspecialization *spec in specs) { [self.storage deleteobjects:spec]; } [self.storage deleteobject:job]; [self.storage commitwritetransaction];
if want delete specializations containing job being deleted if still have other jobs:
[self.storage beginwritetransaction]; [self.storage deleteobjects:[job linkingobjectsofclass:mbspecialization.classname forproperty:@"jobs"]]; [self.storage deleteobject:job]; [self.storage commitwritetransaction];
alternatively, may want clean specializations no longer have jobs after 1 deleted:
[self.storage beginwritetransaction]; [self.storage deleteobject:job]; [self.storage deleteobjects:[mbspecialization objectsinrealm:self.storage where:@"jobs.@count = 0"]]; [self.storage commitwritetransaction];
Comments
Post a Comment