I've done something like this, which may not be an exact fit, but may help.
I had a similar setup for a chat application. Every message had a sender and recipient, but obviously the current user was one of the two. So I stored all the messages in their own collection, where the name of the collection was the id of the non-current-user.
If using YapDatabase, you'd use the YapCollectionsDatabase class.
== Edit ==
You'd start by making your message & user class:
@interface Message : NSObject <NSCoding> ...
@property (...) NSString *sender_id;
@property (...) NSString *recipient_id;
@property (...) NSString *user_id; // sender_id || recipient_id (non-current-user)
@property (...) NSDate *timestamp;
...
@end
@interface User : NSObject <NSCoding>
@property (...) NSString *user_id;
...
@end
Now to store these objects in the database.
We start with YapCollectionsDatabase. This is a collection/key/value store. So when a new message arrives, we just store it in the proper collection
[dbConnection readWriteWithBlock:^(YapCollectionsDatabaseReadWriteTransaction *transaction){
[transaction setObject:messsage
forKey:uuid
inCollection:message.user_id
withMetadata:message.timestamp];
}];
So each message is stored separately. But it is placed in a collection with all other messages in the conversation. Further, adding a new message is fast because you're just adding a single row to the database.
Internally the sqlite database looks like this: |collection|key|object|metadata|
To find the number of conversations, or get the userIds for the conversations:
[dbConnection readWithBlock:^(YapCollectionsDatabaseReadTransaction *transaction){
conversationCount = [transaction numberOfCollections];
conversationUserIds = [transaction allCollections];
}];
To get the number of messages in a conversation, or the ids of the messages:
[dbConnection readWithBlock:^(YapCollectionsDatabaseReadTransaction *transaction){
messageCount = [transaction numberOfKeysInCollection:user_id];
messageIdsSorted = [transaction allKeysOrdered:NSOrderedAscending
inCollection:user_id];
}];
To delete old messages from the database:
[dbConnection readWriteWithBlock:^(YapCollectionsDatabaseReadWriteTransaction *transaction){
[transaction removeObjectsEarlierThan:twoWeeksAgo inCollection:user_id];
}];
Hope this helps.