iOS上的键值存储数据库 [英] key value store database on iOS

查看:115
本文介绍了iOS上的键值存储数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YapDatabase 是创建iOS上的键值存储数据库的一种尝试。。 p>

但是,我没有使用它的经验,想了解使用它而不是诸如Core Data或FMDB之类的东西是否值得。



我遇到的一个关键问题是:如何管理与此数据库之间的对象关系?



如果可以避免对象关系,我会-寻找有关如何解决与另一个对象具有多对多关系的对象问题的建议或数据库设计技巧。



让我们考虑一个用例(这只是一个示例,以帮助解决此问题):



sender<<->消息<<->>收件人

 发件人具有:
photo_data,
名称,
性别,
年龄,
电子邮件,
用户名,
twitter_id,
facebook_id,
sender_id

收件人有一个:
photo_data,
名称,
性别,
年龄,
电子邮件,
用户名,
twitter_id,
facebook_id,
receiver_id

消息具有:
文本,
rich_text,
picture_data,
voice_data,
shared_url,
message_id

我曾考虑过使用message_id,sender_id和receive_id关联每个模型,但是在那里

解决方案

我已经做了类似的事情,虽然可能不太合适,但可能会有所帮助。 / p>

我为聊天应用程序设置了类似的设置。每个消息都有一个发送者和接收者,但是显然当前用户是两者之一。因此,我将所有消息存储在他们自己的集合中,集合的名称是非当前用户的ID。



如果使用YapDatabase,您将使用YapCollectionsDatabase类。



==编辑==



您首先要使消息&用户类:

  @interface消息:NSObject< NSCoding> ... 

@property(...)NSString * sender_id;
@property(...)NSString * recipient_id;
@property(...)NSString * user_id; // sender_id || receive_id(非当前用户)

@property(...)NSDate *时间戳;
...
@end

@interface用户:NSObject< NSCoding>

@property(...)NSString * user_id;
...
@end

现在将这些对象存储在数据库中



我们从YapCollectionsDatabase开始。这是一个集合/键/值存储。因此,当收到新邮件时,我们将其存储在适当的集合中

  [dbConnection readWriteWithBlock:^(YapCollectionsDatabaseReadWriteTransaction * transaction){ 
[交易setObject:messsage
forKey:uuid
inCollection:message.user_id
withMetadata:message.timestamp];
}];

因此,每条消息都是分开存储的。但是它与会话中的所有其他消息一起放置在一个集合中。此外,添加新消息的速度很快,因为您只需向数据库中添加一行即可。



内部的sqlite数据库如下所示:
| collection | key | object |元数据|



查找会话数或获取会话的用户ID:

  [dbConnection readWithBlock:^(YapCollectionsDatabaseReadTransaction * transaction){
sessionCount = [transaction numberOfCollections];
sessionUserIds = [交易allCollections];
}];

要获取对话中的消息数或消息ID:

  [dbConnection readWithBlock:^(YapCollectionsDatabaseReadTransaction * transaction){
messageCount = [transaction numberOfKeysInCollection:user_id];
messageIdsSorted = [交易allKeysOrdered:NSOrderedAscending
inCollection:user_id];
}];

要从数据库中删除旧消息:

  [dbConnection readWriteWithBlock:^(YapCollectionsDatabaseReadWriteTransaction * transaction){
[transaction removeObjectsEarlierThan:twoWeeksAgo inCollection:user_id];
}];

希望这会有所帮助。


One example of an effort to create a key value store database on the iOS is YapDatabase.

However, I have no experience with it, and would like to understand whether it's worth it to use this, instead of something like Core Data or FMDB.

One critical question I have is: How do I manage object relationships with this database?

If I can avoid object relationships, I'm looking for suggestion or database design tips on how to solve the problem of an object that has many-to-many relationship to another object.

Let's consider one use case (this is just an example, to help solving this problem):

sender <<->> message <<->> recipient

sender has a: 
    photo_data, 
    name, 
    gender, 
    age, 
    email, 
    username, 
    twitter_id, 
    facebook_id, 
    sender_id

recipient has a: 
    photo_data, 
    name, 
    gender, 
    age, 
    email, 
    username, 
    twitter_id, 
    facebook_id, 
    recipient_id

message has a:
    text, 
    rich_text, 
    picture_data, 
    voice_data, 
    shared_url, 
    message_id

I have thought about using the message_id, sender_id, and recipient_id to relate each model, but is there a better way?

解决方案

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.

这篇关于iOS上的键值存储数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆