Define relationship type in Core Data

Hello I'd like to set a relationship between two messages by adding an additional relationship type.

Example:

message2 isDuplicate message3  
message3 isSuccessor message4
message4 isPredecessor message3
etc.

The number of types is not fixed so adding an relationship for each type is not a real option. I SQL would do something like:

Message{_id_, text}
msgRelation{_msgID1_, _msgID2_, type} 
//_msgID1_, _msgID2_ foreign keys to Message.id

I hope someone can help.

Answers


It sounds like you need to add an entity to model the relationship itself.

RelateType{
  type:string // ...isDuplicate, isSuccessor, isPredecessor etc
  fromMessage<<-->Message.toMessages
  toMessage<<-->Message.fromMessages
}

Message{
  title:string
  toMessages<-->>RelateType.fromMessages
  fromMessages<-->>RelateType.toMessages
}

The RelateType will form a bridge between any two Messages. The RelateType.type relationship will let you model any arbitrary kind of relationship. To find any particular Message objects relationships by type you just:

NSString *relateType=@"isDuplicate";
NSPredicate *p=[NSPredicate predicateWithFormat:@"type == %@", relateType];
NSSet *duplicateMsgs=[msgObj filteredSetUsingPredicate:p];

Alternatively, you can do a fetch on the RelateType entity to find all of a certain type of relationship e.g. all isDuplicate relationships.

However, usually if you find yourself doing something like this, it's because you've tried to shoehorn a SQL schema into Core Data model which is always a mistake.


Need Your Help

how to create a file in Mac cloud drive using python

python unix icloud

I am trying to create a file in my cloud drive in a python code. When i create a file at this location"/Users/agauravdeep/Library/Mobile Documents/com~apple~CloudDocs/Untitled.rtf

Android Facebook Api Exception - remote_app_id does not match stored id

android facebook login app-id

I have an issue as said in the title of this post. I am unable to login.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.