Unexpectedly large Realm file size

后端 未结 3 1429
北海茫月
北海茫月 2020-12-11 06:31

This question is about using two different ways to insert objects into a Realm. I noticed that the first method is a lot faster, but the size result is huge comparing with t

3条回答
  •  半阙折子戏
    2020-12-11 06:56

    Thanks everyone. I found an optimized way to do the task using your tips. I just did the .write, in batches instead of sending all the content in a single operation. Follows some data to compare:

    Batch Size (Objects) | File Size (mb)

    10.000 = 23.1mb
    5.000 = 11.5mb
    2.500 = 5.8mb
    1.250 = 4.2mb
    625 = 3.7mb
    300 = 3.7mb
    100 = 3.1mb
    50 = 3.1mb
    10 = 3.4mb
    5 = 3.1mb

    So in my humble opinion working with batches of 1000 is the best size / speed for this case.

    Here is the code i used for this test. The only thing changed was the for 1...XXX interation.

        let realm = try! Realm(fileURL: banco_url!)
    
        var objects = [realm_obj]()
        var ids = incrementID()
    
        while (ids < 40000) {
    
            for i in 1...5{
    
                let new_realm_obj = realm_obj(value: ["id" : ids,
                                                    "a": "123",
                                                    "b": 12.12,
                                                    "c": 66,
                                                    "d": 13.13,
                                                    "e": 0.6,
                                                    "f": "01100110",
                                                    "g": someDateTime,
                                                    "h": 3])
                objects.append(new_realm_obj)
                ids += 1 
            }
    
            try! realm.write {
                realm.add(objects)
            }
        }
    

提交回复
热议问题