Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we drop and recreate the entire collection without impacting the reading thread? #156

Open
vtharmalingam opened this issue Feb 10, 2024 · 7 comments

Comments

@vtharmalingam
Copy link

Hi,

I have a use case wherein I need to drop my table/collection every 30 minutes (or so) and recreate it with new data. The important requirement is that the other thread that reads should not run out of data (from the said table) at any moment. The main thread should drop and recreate the collection without impacting the reading thread, which has it's own realm instance.

So I have two Qs here:

  1. Can I do the drop operation inside like this: realm_instance. rite([&] { //drop the collection } ?
  2. Secondly, I am unable to figure out a specific function that drops the given collection. I know there exists a function called "remove," but that only removes one record and not the entire table.

BTW, my use case can even let me build a new collection while loading the new data, so that can swap the name post-loading, if that is an option available too.

Please advise. Thanks for your help in advance.

Regards,
Tharma

@vtharmalingam
Copy link
Author

@leemaguire : Can you please throw some lights on the above? I am actually stuck with that. Thanks for your help.

@leemaguire
Copy link
Contributor

Hi @vtharmalingam, that function is not currently exposed. I'll get a PR up that exposes the functionality soon.

@vtharmalingam
Copy link
Author

Thank you very much for the update, @leemaguire. Really appreciated.

@vtharmalingam
Copy link
Author

@leemaguire :
Could you please advise me if there's any method available today to clear a collection during a write operation?

@leemaguire
Copy link
Contributor

For the meantime you can do this until we expose the correct API to remove all objects:

auto res = realm.objects<AllTypesObject>();
            realm.write([&] {
                while (realm.objects<AllTypesObject>().size() > 0) {
                    auto obj = realm.objects<AllTypesObject>()[0];
                    realm.remove(obj);
                }
            });

@vtharmalingam
Copy link
Author

Sure, this is really helpful. Thank you @leemaguire, as always!

@turninX
Copy link

turninX commented Oct 15, 2024

Hi,
I wonder if there is a exposed API to remove all objects in new version?
I tried this and the data was indeed cleared, but I found that the size of the database file did not decrease. Is there any way to reduce the file size while clearing the data?

auto res = realm.objects<AllTypesObject>();
            realm.write([&] {
                while (realm.objects<AllTypesObject>().size() > 0) {
                    auto obj = realm.objects<AllTypesObject>()[0];
                    realm.remove(obj);
                }
            });

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants