在 JavaScript 中初始化 JSON 存储:
function doJSTestInit() {
var peopleCollectionName = 'people';
var collections = {
// Object that defines the 'people' collection.
people : {
// Object that defines the Search Fields for the 'people'
// collection.
searchFields : {
mid1 : 'integer', //模块/模块组主键ID
level : 'integer', //组件层级
moduleId : 'string', //模块/模块组业务ID
appId : 'integer', //应用ID
moduleType : 'string', //功能模块F,登陆模块
// status : 'string', //模块状态 上架: U 下架: D 暂停:P
// moduleName : 'string', //模块/模块组名称
// iconUrl : 'string', //图标URL
// updateStatus : 'string', //强制更新,提示更新
// downloadStatus : 'string', //必选下载,可选下载
// engineType : 'string', //模块类别 EMP WL
// pauseReason : 'string', //暂停原因
// moduleUrl : 'string', //模块存放路径UR
// md5 : 'string', //模块资源包MD5码
// needLogin : 'string', //是否需要登录才能使用模块功能,是 : Y, 否 : N
}
},
};
WL.JSONStore.destroy()
.then(function() {
// Open the collection
return WL.JSONStore.init(collections);
}).then(function() {
// Data to add, you probably want to get
// this data from a network call (e.g. Worklight Adapter).
var data = [ {
name : 'carlos',
age : 10
} ];
// Optional options for add.
var addOptions = {
// Mark data as dirty (true = yes, false = no), default true.
markDirty : true
};
// Get an accessor to the people collection and add data.
return WL.JSONStore.get(peopleCollectionName).add(data, addOptions);
});
}
然后尝试在 iOS Native Code 中打开这个 JSON Store:
-(void)onActionReceived:(NSString *)action withData:(NSDictionary *)data{
NSLog(@"LoginScreenViewController :: onActonReceived :: %@", action);
if ([action isEqualToString:@"testJSONInit"]){
NSError* error = nil;
// [[JSONStore sharedInstance]destroyDataAndReturnError:&error];
people = [[JSONStoreCollection alloc]initWithName:@"people"];
[people setSearchField:@"mid1" withType:JSONStore_Integer]; //模块/模块组主键ID
[people setSearchField:@"level" withType:JSONStore_Integer]; //组件层级
[people setSearchField:@"moduleId" withType:JSONStore_String]; //模块/模块组业务ID
[people setSearchField:@"appId" withType:JSONStore_Integer]; //应用ID
[people setSearchField:@"moduleType" withType:JSONStore_String]; //功能模块F,登陆模块
// [people setSearchField:@"status" withType:JSONStore_String]; //模块状态 上架: U 下架: D 暂停:P
// [people setSearchField:@"moduleName" withType:JSONStore_String]; //模块/模块组名称
// [people setSearchField:@"iconUrl" withType:JSONStore_String]; //图标URL
// [people setSearchField:@"updateStatus" withType:JSONStore_String]; //强制更新,提示更新
// [people setSearchField:@"downloadStatus" withType:JSONStore_String]; //必选下载,可选下载
// [people setSearchField:@"engineType" withType:JSONStore_String]; //模块类别 EMP WL
// [people setSearchField:@"pauseReason" withType:JSONStore_String]; //暂停原因
// [people setSearchField:@"moduleUrl" withType:JSONStore_String]; //模块存放路径UR
// [people setSearchField:@"md5" withType:JSONStore_String]; //模块资源包MD5码
// [people setSearchField:@"needLogin" withType:JSONStore_String]; //是否需要登录才能使用模块功能,是 : Y, 否 : N
//Open the collections.
[[JSONStore sharedInstance] openCollections:@[people] withOptions:nil error:nil];
}else if ([action isEqualToString:@"testJSON"]){
int dataAddedToThePeopleCollection = [[people addData:@[@{@"moduleId" : @"carlos", @"level" : @20}] andMarkDirty:YES withOptions:nil error:nil] intValue];
}
}
错误将在下面被捕获:
2014-10-20 20:37:57.517 JStoreTest[64745:879970] [ERROR] [JSONSTORE] -[JSONStore _provisionCollection:withSearchFields:withAdditionalSearchFields:withUsername:withPassword:withDropFirst:error:] in JSONStore.m:724 :: Error: JSON_STORE_EXCEPTION, code: -2, username: jsonstore, accessor username: jsonstore, collection name: people, searchFields: { appId = integer; level = integer; mid1 = integer; moduleId = string; moduleType = string; }, additionalSearchFields: { }
code: -2 means , PROVISION_TABLE_SEARCH_FIELDS_MISMATCH
但字段完全匹配。如果我只使用 3 个字段,它会完美地工作,如果超过 3 个字段,则会出现上述错误。为什么?这是一个错误吗?谢谢。
最佳答案
这行得通,谢谢 Daniel。
这并没有直接解决你的问题,但我想补充一点,在你用 Javascript 初始化后,你不必在 native 中再次调用 openCollection();你可以直接调用 [[JSONStore sharedInstance] getCollectionWithName:@"people"];它将返回已经由 Javascript 初始化的集合。 – Daniel A. González
关于如果在 JS 中初始化,iOS JSON Store 打开失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26466179/