You are viewing Skygear v1 Documentation.Switch to Skygear v0 Documentation

Local Storage (Offline)


Record storage relies on Query

Add SKYContainerDelegate to your controller.

- (void)container:(SKYContainer *)container didReceiveNotification:(SKYNotification *)notification
    // ...
    [[SKYRecordStorageCoordinator defaultCoordinator] handleUpdateWithRemoteNotification:notification];

func container(_ container: SKYContainer!, didReceive notification: SKYNotification!)
	// ...
	SKYRecordStorageCoordinator.default().handleUpdate(withRemoteNotification: notification)

Creating a record storage

SKYQuery *query = [[SKYQuery alloc] initWithRecordType:@"note" predicate:nil];
SKYRecordStorageCoordinator *coordinator = [SKYRecordStorageCoordinator defaultCoordinator];
SKYRecordStorage* recordStorage = [coordinator recordStorageWithDatabase:self.database
                                                                  query:query options:nil];
let query = SKYQuery(recordType: "note", predicate: nil)
let coordinator = SKYRecordStorageCoordinator.default()
let recordStorage = coordinator.recordStorage(with: SKYContainer.default().privateCloudDatabase,
											  query: query, options: nil)

Saving records

SKYRecord *note = [SKYRecord recordWithRecordType:@"note"];
note[@"content"] = @"record storage is fun!";
[recordStorage saveRecord:note];
let note = SKYRecord(recordType: "note")
note.setValue("record storage is fun!", forKey: "content" as NSCopying!)

Deleting records

[recordStorage deleteRecord:noteToDelete];

Fetching records

SKYRecord *record = [recordStorage recordWithRecordID:recordID];
let record = recordStorage.record(with: recordID)

Querying records

Without Predicate

for (SKYRecord *note in [recordStorage recordsWithType:@"note"]) {
    // do something with note
let notes = recordStorage.records(withType: "note")
for note in notes as! [SKYRecord]{
    // do something with note

With Predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"done == false"];
NSArray *records = [recordStorage recordsWithType:@"todo"
for (SKYRecord *note in records) {
    // do something with note
let predicate = NSPredicate(format: "done == false")
let records = recordStorage.records(withType: "todo", predicate: predicate, sortDescriptors: nil)
for note in records as! [SKYRecord] {
    // do something with note

Listening to change event

[[NSNotificationCenter defaultCenter] addObserverForName:SKYRecordStorageDidUpdateNotification
                                                   queue:[NSOperationQueue mainQueue]
                                              usingBlock:^(NSNotification *note) {
                                                  self.notes = [self.categoryStorage recordsWithType:@"note"];
                                                  [self.tableView reloadData];
	forName: Notification.Name.SKYRecordStorageDidUpdate,
	object: recordStorage,
	queue: OperationQueue.main) { (note) in
   		notes = categoryStorage.records(withType: "note")