在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存储:
-(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] [错误] [JSONSTORE] - [JSONStore _provisionCollection:withSearchFields:withAdditionalSearchFields:withUsername:withPassword:withDropFirst:error:]在JSONStore.m中:724: :错误:JSON_STORE_EXCEPTION,代码:-2,用户名:jsonstore,访问者用户名:jsonstore,集合名称:people,searchFields:{ appId =整数; level =整数; mid1 =整数; moduleId = string; moduleType = string; },additionalSearchFields:{ }
代码:-2表示,PROVISION_TABLE_SEARCH_FIELDS_MISMATCH
但这些字段完全匹配。如果我只使用3个字段,那么它可以正常工作,如果超过3个字段,则会出现上述错误。为什么?这是一个错误吗?感谢。
答案 0 :(得分:1)
这并没有直接解决您的问题,但我想补充一点,在初始化Javascript之后,您不必再在本机中调用openCollection();你可以调用[[JSONStore sharedInstance] getCollectionWithName:@“people”];它将返回已经由Javascript初始化的集合。 - DanielA.González
答案 1 :(得分:0)
嗯,在你看来,在你用js创建它们之后,你试图修改本机ios中的搜索字段。即使您只是重新初始化搜索字段,也无法执行此操作。尝试销毁本机iOS中的集合,然后重新初始化搜索字段。