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];
}
}
}];
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…