JSONStore是一个轻量级的文档导向存储系统,使得iOS应用程序能够持久化存储JSON文档。
JSONStore SDK可以通过Cocoapods获取。要安装,请在Podfile中添加以下pods。
platform :ios
pod 'JSONStore'
pod 'sqlite3'
注意,
sqlite3
库是一个静态库,不能与动态框架一起使用。这意味着您将无法使用use_frameworks!
Cocoapods标志。
有关如何启用安全的更多信息,请访问以下博客。
将以下导入添加到您的类中,以开始使用JSONStore
#import <JSONStore/JSONStoreFramework.h>
// Create the collections object that will be initialized.
JSONStoreCollection* people = [[JSONStoreCollection alloc] initWithName:@"people"];
[people setSearchField:@"name" withType:JSONStore_String];
[people setSearchField:@"age" withType:JSONStore_Integer];
// Optional options object.
JSONStoreOpenOptions* options = [JSONStoreOpenOptions new];
[options setUsername:@"hayatashin"]; //Optional username, default 'jsonstore'
[options setPassword:@"deta"]; //Optional password, default no password
// This object will point to an error if one occurs.
NSError* error = nil;
// Open the collections.
[[JSONStore sharedInstance] openCollections:@[people] withOptions:options error:&error];
// Add data to the collection
NSArray* data = @[ @{@"name" : @"saito", @"age": @10} ];
int newDocsAdded = [[people addData:data andMarkDirty:YES withOptions:nil error:&error] intValue];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Add additional find options (optional).
JSONStoreQueryOptions* options = [JSONStoreQueryOptions new];
[options setLimit:@10]; // Returns a maximum of 10 documents, default no limit.
[options setOffset:@0]; // Skip 0 documents, default no offset.
// Search fields to return, default: ['_id', 'json'].
[options filterSearchField:@"_id"];
[options filterSearchField:@"json"];
// How to sort the returned values , default no sort.
[options sortBySearchFieldAscending:@"name"];
[options sortBySearchFieldDescending:@"age"];
// Find all documents that match the query part.
JSONStoreQueryPart* queryPart1 = [[JSONStoreQueryPart alloc] init];
[queryPart1 searchField:@"name" equal:@"shu"];
[queryPart1 searchField:@"age" lessOrEqualThan:@10];
NSArray* results = [people findWithQueryParts:@[queryPart1] andOptions:options error:&error];
// results = @[ @{@"_id" : @1, @"json" : @{ @"name": @"shu", @"age" : @10}} ];
for (NSDictionary* result in results) {
NSString* name = [result valueForKeyPath:@"json.name"]; // shu.
int age = [[result valueForKeyPath:@"json.age"] intValue]; // 10
NSLog(@"Name: %@, Age: %d", name, age);
}
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// Find all documents that match the queries.
NSArray* docs = @[ @{@"_id" : @1, @"json" : @{ @"name": @"kenshin", @"age" : @99}} ];
// This object will point to an error if one occurs.
NSError* error = nil;
// Perform the replacement.
int docsReplaced = [[people replaceDocuments:docs andMarkDirty:NO error:&error] intValue];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Find document with _id equal to 1 and remove it.
int docsRemoved = [[people removeWithIds:@[@1] andMarkDirty:NO error:&error] intValue];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// Count all documents that match the query.
// The default query is @{} which will
// count every document in the collection.
JSONStoreQueryPart *queryPart = [[JSONStoreQueryPart alloc] init];
[queryPart searchField:@"name" equal:@"aiko"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Perform the count.
int countResult = [[people countWithQueryParts:@[queryPart] error:&error] intValue];
// This object will point to an error if one occurs.
NSError* error = nil;
// Perform the destroy.
[[JSONStore sharedInstance] destroyDataAndReturnError:&error];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Check if document with _id '1' is dirty.
BOOL isDirtyResult = [people isDirtyWithDocumentId:1 error:&error];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Check if document with _id '1' is dirty.
int dirtyDocsCount = [[people countAllDirtyDocumentsWithError:&error] intValue];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Remove the collection.
[people removeCollectionWithError:&error];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// This object will point to an error if one occurs.
NSError* error = nil;
// Remove the collection.
[people clearCollectionWithError:&error];
// Get the accessor to an already initialized collection.
JSONStoreCollection* people = [[JSONStore sharedInstance] getCollectionWithName:@"people"];
// These objects will point to errors if they occur.
NSError* error = nil;
NSError* addError = nil;
NSError* removeError = nil;
// You can call every JSONStore API method inside a transaction except:
// open, destroy, removeCollection and closeAll.
[[JSONStore sharedInstance] startTransactionAndReturnError:&error];
[people addData:@[ @{@"name" : @"kyo"} ] andMarkDirty:NO withOptions:nil error:&addError];
[people removeWithIds:@[@1] andMarkDirty:NO error:&removeError];
if (addError != nil || removeError != nil) {
// Return the store to the state before start transaction was called.
[[JSONStore sharedInstance] rollbackTransactionAndReturnError:&error];
} else {
// Commit the transaction thus ensuring atomicity.
[[JSONStore sharedInstance] commitTransactionAndReturnError:&error];
}
[[JSONStore sharedInstance] setEncryption:YES];
请注意,启用加密需要安装来自IBM MobileFirst平台基础的其他附加组件。
// This object will point to an error if one occurs
NSError* error = nil;
// Returns information about files JSONStore uses to persist data.
NSArray* results = [[JSONStore sharedInstance] fileInfoAndReturnError:&error];
// => [{@"name" : @"aion", @"size" : @3072}]
版权所有 IBM公司,2016年。
本文件根据Apache License版2.0(“许可”)授权;除非遵守许可,否则不得使用此文件。您可以从以下位置获取许可副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或在书面协议中同意,否则在不提供任何明示或暗示保证的情况下,根据许可进行分发的软件按“原样”分发,不承担任何类型的保证或条件。请参阅许可,了解许可下管理权限和限制的具体语言。