본문 바로가기
DataBase/IOS - Realm

IOS) Realm - Compact FileSize 그리고 주의할 사항

by 후르륵짭짭 2022. 7. 24.
728x90
반응형

서울 어딘가에서

 

안녕하세요. 후르륵짭짭입니다.

Realm 사용할 때 몇가지 주의할 점이 있는 것 같아서 작성합니다.

사내에서 Realm 담당은 아니지만 궁금한게 있으면 찾아보는 정도라서,,,

블로그 글 본걸 따라한 정도입니다.

 

** Compact File Size **

Realm을 사용하면 할 수록 File 사이즈가 증가합니다. 

그러면 사용자에게 불필요하게 안 좋은 앱경험을 주는것이 되겠지요.

그래서 필요한 것이 압축입니다. 

Realm은 TotalSize와 UsedSize를 알 수 있습니다.

TotalSize는 현 파일 총 용량이고 UsedSize는 현재 가용하고 있는 사이즈 입니다.

위 이미지를 보면 shouldCompactOnLaucn라는 것이 있습니다.

이것은 앱일 실행할 때 Realm파일을 Compact 할 것이냐를 물어보는 것 입니다.

그리고 지금 총 10000개의 파일 저장했을 때 totalSize는 131KB에 UsedSize는 34KB입니다.

이는 파일 사이즈는 큰데 가용유를은 0.5%도 안되는 것 입니다.

그런데 이렇게 shouldCompacOnLaunch에 조건을 걸어주고 가용률이 0.8 미만이라면 압축하도록 했습니다.

그 결과 totalSize는 131에서 69KB로 줄었고 가용률도 50%가 되었습니다.

shouldCompactOnLaucn를 통해서 Realm의 파일 사이즈를 조절 할 수 있습니다.

 

** 앱실행과 Migration의 시간 주의 **

만약 Realm 파일을 변경해야할 경우가 있다면 Migration을 시켜줘야합니다.

위에 처럼 SchemaVersion2에서는 두가지의 필드를 추가한다고 하면 Migration이 필요하게 됩니다.

위의 내용에서 MigrationBlock을 보면 총 10000개의 Migration 작업을 진행하니 시간이 0.2초가 되었고

didFinishLaunching 할 때 시간이 0.1(기본시간) + 0.2(Realm Migration시간) 해서 0.3초가 되었습니다.

즉, Realm의 Migraion 작업을 didFinishLaunching에서 한다면 앱 첫 실행 시작이 늦어질 수 있습니다.

이런 부분은 주의해서 앱 실행 후에 Realm을 시작하도록 해야합니다.

 

** Realm 저장 속도 증가 시키기 **

Realm에 write를 할 때, 배열에 담아서 한번에 저장하면 저장하는 속도를 줄일 수 있습니다.

 

** Realm 역관계 실수 **

class Category: Object {
    @objc dynamic var name : String = ""
    let items = List<Item>()
}

class Item: Object {
    @objc dynamic var title: String = ""
    @objc dynamic var done : Bool = false
    let parentCategory = LinkingObjects(fromType: Category.self, property: "items")
}

역관계를 만들 때는 property에 올바른 property 이름을 작성해야합니다.

 

참고 사이트 : 

Realm 역관계일 때 Crash 발생 

https://stackoverflow.com/questions/63908984/realm-with-swift-5-inverse-relationship-crashes-the-app-on-launch

 

Realm with Swift 5 Inverse relationship crashes the app on launch

I am learning how to use Realm db, as per examples I'm using the following tasks list with 2 classes: Category and Item, and each category may have many items but each item belong to one category as

stackoverflow.com

 

Realm Result를 Array로 변경

https://stackoverflow.com/questions/31100011/realmswift-convert-results-to-swift-array

 

RealmSwift: Convert Results to Swift Array

What I want to implement: class func getSomeObject() -> [SomeObject]? { let objects = Realm().objects(SomeObject) return objects.count > 0 ? objects : nil } How can I return object...

stackoverflow.com

 

CoreData VS Realm

https://agilie.com/blog/coredata-vs-realm-what-to-choose-as-a-database-for-ios-apps

 

CoreData vs Realm: What to Choose as a Database for iOS Apps

Considering database solutions? Don't know what to choose? We have the answer! Click here ➤ and find out the best open source database!

agilie.com

 

Realm 아키텍처 

https://ali-akhtar.medium.com/lightweight-persistence-layer-with-realm-realmswift-part-5-7f5a707e35e8

 

Realm 참고 사이트 

https://ali-akhtar.medium.com/concurrency-multi-threading-in-realm-realmswift-part-4-2345deabe512

 

Concurrency/Multi threading in Realm (RealmSwift Part 4)

In this part we will cover following topics

ali-akhtar.medium.com

https://ali-akhtar.medium.com/challenges-problems-in-real-application-realmswift-part-8-6832049bd7de

 

Challenges/Problems In Real Application(RealmSwift Part 8)

In this blog I will cover the possible challenges or problems you can face while using realm in real application. It is mandatory that you…

ali-akhtar.medium.com

 

728x90
반응형

댓글