Pavel Hrybouski
Pavel Hrybouski

Reputation: 23

What the best way for storing chat messages and dialogs in IOS

I'm writing an App which consists of chat. What is the best practice for storing messages, which sometimes consist of images and dialogs? On the one hand it would be better to save them in Core Data, but the user can get messages from server. On the other hand I can use cache, but nobody knows how long messages should be stored. I ineffectually tried to understand how the vk.com App works with this. How would you handle this? Any samples of Objective-C code are welcome.

Upvotes: 1

Views: 1621

Answers (2)

Mike Critchley
Mike Critchley

Reputation: 1682

I have a social app. I keep chat messages in Core Data in a relationship with the chat room to which they belong, so it's super easy to fetch a set of messages via the managed object of the chatroom. If notifications come in, I'll update the messages in the background, and on launch of a chatroom, I'll ping the server to get any messages that are newer than my most recent message.

For media such as images, you can store them as binary data in your messages entity up to no more than 1MB. After that, you should store them in the directory and just keep the URL as property in your message entity.

Another real advantage of core data is that it makes it easy to manage your memory. You can fetch any messages / associated media older than a certain time and just delete them all. Since your server is the main repository for your messages, should a user happen to open that chat room again, you can just download the last batch of messages when needed (and store them again to Core Data). But the actual access of your objects is all NSPredicate based, which makes it super easy to query.

Upvotes: 5

Clown
Clown

Reputation: 183

If you don't have an Web Service you can use Firebase from Google. Here is a good sample project about cloud messaging it also has send image feature. And also free Notification service also provided by Google. Sample code and project can be found here. It stores messages over Realtime Database

Upvotes: 0

Related Questions