Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear complete Realm Database

Tags:

ios

realm

I'm playing around with realm (currently 0.85.0) and my application uses the database to store user-specific data such as the contacts of the current user. When the user decides to log out I need to remove every single bit of information about the user and the most obvious, simple and clean thing in my opinion would be to wipe the complete realm. Unfortunately, the Cocoa lib doesn't provide that functionality.

Currently, I'm stuck with the following

RLMRealm *realm = [RLMRealm defaultRealm]; [realm beginWriteTransaction]; [realm deleteObjects:[MyRealmClass1 allObjectsInRealm:realm]]; [realm deleteObjects:[MyRealmClass2 allObjectsInRealm:realm]]; [realm deleteObjects:[MyRealmClass3 allObjectsInRealm:realm]]; [realm commitWriteTransaction]; 

any better ideas?

thanks

like image 212
floriankrueger Avatar asked Sep 26 '14 19:09

floriankrueger


People also ask

How do I clear Realm database react native?

Delete All Objects in a Realm To delete all objects from the realm, call Realm. deleteAll() inside of a write transaction. This clears the realm of all object instances but does not affect the realm's schema.

How do you delete a Realm file?

To completely delete the Realm file from disk and start from scratch, it's simply a matter of using NSFileManager to manually delete it. For example, to delete the default Realm file: NSFileManager. defaultManager().

What database does Realm use?

In a current GlobalLogic project, we are managing a 3MB database in SQLite running on Android.

What is Realm Nosql database?

Realm is an open source object database management system, initially for mobile operating systems (Android/iOS) but also available for platforms such as Xamarin, React Native, and others, including desktop applications (Windows), and is licensed under the Apache License.


1 Answers

Update:

Since posting, a new method has been added to delete all objects (as jpsim has already mentioned):

// Obj-C [realm beginWriteTransaction]; [realm deleteAllObjects]; [realm commitWriteTransaction];   // Swift try! realm.write {   realm.deleteAll() } 

Note that these methods will not alter the data structure; they only delete the existing records. If you are looking to alter the realm model properties without writing a migration (i.e., as you might do in development) the old solution below may still be useful.

Original Answer:

You could simply delete the realm file itself, as they do in their sample code for storing a REST response:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     //...      // Ensure we start with an empty database     [[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];      //... } 

Update regarding your comment:

If you need to be sure that the realm database is no longer in use, you could put realm's notifications to use. If you were to increment an openWrites counter before each write, then you could run a block when each write completes:

self.notificationToken = [realm addNotificationBlock:^(NSString *notification, RLMRealm * realm) {     if([notification isEqualToString:RLMRealmDidChangeNotification]) {         self.openWrites = self.openWrites - 1;          if(!self.openWrites && self.isUserLoggedOut) {             [[NSFileManager defaultManager] removeItemAtPath:[RLMRealm defaultRealmPath] error:nil];         }     } }]; 
like image 132
uɥƃnɐʌuop Avatar answered Sep 28 '22 05:09

uɥƃnɐʌuop