Default.realm size does not decrease after deletion in RLMealm deleteObject

Asked 2 years ago, Updated 2 years ago, 87 views

Among TimelineItems(RLMOobject) nested in Timeline(RLMOobject),
I wrote a code to delete data that is older than 12 hours ago in bulk
The size of the database file (default.realm) does not decrease.

The default.realm size is
· Immediately after installing the application, dozens of KB
·After using the app for one day (24 hours), 240MB (increase 10MB evenly every hour)
Assume that

Assumed
if data older than 12 hours were deleted in bulk on the RLMealm deleteObject ·60MB (12/24=50%) after batch deletion
It should be, but it's actually only about 100MB.

Below is the code you want to delete.

RLMealm*realm=[RLMealm defaultRealm];

// Get all timelines
RLMResult* timelines= [Timeline allObjects]    

// Scan Timeline
for (Timeline*timeline in timelines) {

    // Obtain data that is older than 12 hours ago among nested models in timeline
    CGFloat hours = 12.0f;
    NSTimeInterval secs=hours*60.0f*60.0f;
    NSDate* priorityDate= [NSDate dateWithTimeIntervalSinceNow:secs];
    RLMResults*timelineItems=[timeline.timelineItems objectsWhere:@"(create_date<=%@), priorityDate];

    Realm beginWriteTransaction;

    // Delete nested child data in bulk
    for (TimelineItem*timelineItem in TimelineItems) {

        // Custom methods for deleting locally stored cache data (e.g., images) and relationships
        timelineItem invalidate;

        // Remove RLMobject
        realm deleteObject:timelineItem;

        // Verify that it has been deleted
        NSLog(@"isInvalidated:%@",@(timelineItem.isInvalidated); // isInvalidated:1
    }

    realm commitWriteTransaction;
}

If you set the hours in the above code to 0 and delete all of them, it should return to tens of KB, but it didn't really happen.

Is the default.realm specification that the discarded data remains?

Actual projects can be more complex or
The model definition also comes with multiple strings in addition to the above models, so
There may be problems around the relationship, but
As far as Realm Browser confirms, the TimelineItem instance has been successfully deleted.

The same phenomenon has occurred in both the actual machine and the simulator.

I would appreciate your reply regarding the above phenomenon.
Thank you for your cooperation.

objective-c realm

2022-09-30 19:22

1 Answers

Realm deletes the data, but the original space is left for reuse.

Note that the Realm file size remains intact for efficient disk space reuse.

https://realm.io/jp/docs/swift/latest/ #section-25

To reduce the file size, run the compaction.
The compaction is performed by copying it as a new file using the -RLMealm writeCopyToPath:error: method.
After copying the file, the unnecessary space is completely removed.

https://realm.io/docs/objc/latest/api/Classes/RLMRealm.html#/c:objc(cs)RLMealm(im)writeCopyToPath:error:


2022-09-30 19:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.