Local Persistent Store is a Key Value store for iOS. It's written in Swift but fully compatable with Objective-C.
Open the store and prepare it for writing:
NSError *error = nil;
if (![[LOPStore defaultStore] openAndReturnError:&error]) {
// Super good error handling
}Write a value:
NSString *name = @"Loper";
[[LOPStore defaultStore] setObject:name forKey:@"project_name" inScope:nil];Reading a value:
NSString *name = [[LOPStore defaultStore] stringForKey:@"project_name" inScope:nil];If the value isn't found nil will be returned.
Scopes allow you to manage a group of keys. E.G.
[[LOPStore defaultStore] setObject@"example@example.com" forKey:@"email" inScope:@"logged_in"];
// User logs out
NSError *error = nil;
if (![[LOPStore defaultStore] deleteScope:@"logged_in" error:&error]) {
// Super good error handling
}
NSString *email = [[LOPStore defaultStore] stringForKey:@"email" inScope:@"logged_in"];
// email == nilThis will delete all values with the logged_in scope.
If nil is passed to scope then [LOPStore scope] will be used. Keys MUST be unique for a scope. So setting foo : nil a second time will replace the original value in the store. But setting foo : bar will insert a new value into the bar scope and keep the default scope value intact.
Because Loper is backed by a SQLite database occasionally you'll need to run cleanup to repack the database file if your keys are volatile.
NSError *error = nil;
if (![[LOPStore defaultStore] cleanupAndReturnError:&error]) {
// Super good error handling
}This operation can be slow and should occure on a background thread. All reads & writes to the store will be blocked until this operation is completed.