我想知道如何在CoreData實體上進行對象比較。 假設我有兩個實體,有多對多的關係。瞭解CoreData中的對象創建和比較
EntityA {
prop1
entityB <-- to-many relationship
}
EntityB {
.... properties
entityA <-- one-to-one relationship
}
假如我想要檢索具有一定PROP1與NSPredicate的實體,爲簡單起見,假設只有一種EntityA滿足這樣的條件:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"prop1=%@",x];
EntityA *entityA = //execute fetch
現在我想檢索所有EntityB傳遞entityA以此爲標準:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"EntityA=%@",entityA];
EntityB *entityB = //execute fetch
EntityA *parentA = entityB.entityA;
在這一點上考慮,我沒有覆蓋任何方法,但EntityA和EntityB只是延長NSManagedObject,這將是重這種說法?
if(entityA==parentA) NSLog(@"YES");
else NSLog("NO");
如果是「是」,爲什麼會出現這種結果?我在同一個執行過程中有多少個對象?例如,使用任何持久層我知道在Java中將導致兩個不同的對象,只有當我重寫相關類的equals方法並在其中實現某些邏輯時,比較結果爲「YES」,例如:
if(objA.getRegistrationNumber()==objA.getRegistrationNumber()) return true;
在這種情況下,我不知道CoreData是否正在做某種對象緩存和重用。
感謝