App performance become slow after added realm
See original GitHub issueGoals
Our app is a chat app , we are going to use local realm database to save all message and we are using AsyncStorage now. We think realm is better after we read the doc, so we want to use it.
Expected Results
All message saved in realm database and encrypted. Chatroom will update and show the messages from database.
Actual Results
Messages are displayed, but the app become very slow for any actions.
Steps to Reproduce
Code Sample
This is the realmHelper.js
import Realm from 'realm'
export const DB_CHATROOM = 'ChatRoom'
export const DB_MESSAGE = 'Message'
export const DB_USER = 'User'
export const chatRoomSchema = {
name: DB_CHATROOM,
primaryKey: 'id',
properties: {
id: { type: 'string' },
userId: { type: 'string' },
messages: { type: 'list', objectType: DB_MESSAGE },
}
}
export const messageSchema = {
name: DB_MESSAGE,
primaryKey: '_id',
properties: {
_id: { type: 'string' },
type: { type: 'string' },
text: { type: 'string' },
createdAt: { type: 'date', indexed: true },
user: { type: DB_USER },
}
}
export const userSchema = {
name: DB_USER,
primaryKey: '_id',
properties: {
_id: { type: 'string' },
name: { type: 'string' },
avatar: { type: 'string', optional: true },
}
}
const realmConfig = {
path: 'theartalke.realm',
schema: [chatRoomSchema, messageSchema, userSchema],
schemaVersion: 1,
}
export const queryOrCreateChatRoom = (chatRoomId, userId) => new Promise((resolve, reject) => {
Realm.open(realmConfig).then(realm => {
realm.write(() => {
let existQueryChatRoom = realm.objectForPrimaryKey(DB_CHATROOM, chatRoomId)
if (!existQueryChatRoom) {
existQueryChatRoom = realm.create(DB_CHATROOM, {
id: chatRoomId,
userId: userId,
})
}
resolve(existQueryChatRoom)
})
}).catch(error => reject(error))
})
export const insertMessage = (chatRoom, incomeMessage) => new Promise((resolve, reject) => {
Realm.open(realmConfig).then(realm => {
realm.write(() => {
let existMessage = realm.objectForPrimaryKey(DB_MESSAGE, incomeMessage._id)
if (!existMessage) {
let existUser = realm.objectForPrimaryKey(DB_USER, incomeMessage.user._id)
if (!existUser) {
existUser = realm.create(DB_USER, {
_id: incomeMessage.user._id,
name: incomeMessage.user.name,
avatar: incomeMessage.user.avatar,
})
}
existMessage = realm.create(DB_MESSAGE, {
_id: incomeMessage._id,
type: incomeMessage.type,
text: incomeMessage.text,
createdAt: incomeMessage.createdAt,
user: existUser,
})
chatRoom.messages.push(existMessage)
}
resolve()
})
}).catch(error => reject(error))
})
we create or get the chatroom first.
const chatRoom = await realmHelper.queryOrCreateChatRoom(this.props.chatRoomId,
this.props.userInfo.userId)
insert the new message
realmHelper.insertMessage(this.props.chatRoom, incomeMessages)
show the messages using GiftedChat ( FlatList )
<GiftedChat
messages={Array.from(this.props.chatRoom.messages.sorted([['createdAt', true], ['_id', true]]))}
....
/>
Version of Realm and Tooling
- Realm JS SDK Version: 2.15.3
- React Native: 0.56.1
- Client OS & Version: IOS 11.4 and android 8.0
- Which debugger for React Native: both tried
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Improving Realm notification performance - Stack Overflow
I'm trying to improve the performance of Realm writes and notifications in my app. When the number of Items grows (100k+) Realm ......
Read more >The Realm API is Optimized for Performance & Low Memory ...
Learn about the Realm API classes that allow for performant and robust apps.
Read more >Best Practices: Performance and Monitoring for Realm Sync
You are currently starting with Realm Sync and want to set it up for success for your production applications ? Great, in the...
Read more >Common sources of slow performance for a canvas app
To review the app design as a possible source of slow app performance, monitor the app by using Monitor. Check which data calls...
Read more >Concurrency/Multi threading in Realm (RealmSwift Part 4)
The consequences could be dramatic. For example, your application could be killed by Apple's watchdog for taking too long to launch and this ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Just briefly looking at the code, you probably don’t want to open the Realm every time you want to insert an object - instead save it as a class-level variable upon first opening it and work with that single instance.
Im newbie. You can show code example for me!