JSONStore 1.3.0

JSONStore 1.3.0

测试已测试
Lang语言 Obj-CObjective C
许可证 Apache 2
发布最后发布2016年4月

Nana Amfo维护。



JSONStore 1.3.0

  • IBM Bluemix移动服务,Anton Aleksandrov 和 Nana Amfo

适用于iOS的JSONStore

JSONStore是一个轻量级的文档导向存储系统,使得iOS应用程序能够持久化存储JSON文档。

功能

  • 一个简单的API,使得开发者可以添加、存储、替换和搜索文档,无需记住查询语法
  • 能够跟踪本地更改

安装

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

除非适用法律要求或在书面协议中同意,否则在不提供任何明示或暗示保证的情况下,根据许可进行分发的软件按“原样”分发,不承担任何类型的保证或条件。请参阅许可,了解许可下管理权限和限制的具体语言。