我正在尝试在iOS本机代码上获取AsyncStorage。所以这是我的代码
- (void)jsonFromLocalRNStrogeForKey:(NSString *)key completion:(void (^)(NSDictionary * _Nullable, NSError * _Nullable))completion {
RCTResponseSenderBlock rnCompletion = ^(NSArray *response) {
NSString *jsonAsString;
if (response.count > 1) {
NSArray *response1 = response[1];
if (response1.count > 0) {
NSArray *response2 = response1[0];
if (response2.count > 1) {
jsonAsString = response2[1];
}
}
}
@try {
NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
NSDictionary *json = [
NSJSONSerialization
JSONObjectWithData:jsonAsData
options:NSJSONReadingMutableContainers
error:&error
];
completion(json, error);
}
@catch (NSException *exception) {
NSLog(@"error: %@", exception.reason);
NSMutableDictionary * info = [NSMutableDictionary dictionary];
[info setValue:exception.name forKey:@"ExceptionName"];
[info setValue:exception.reason forKey:@"ExceptionReason"];
[info setValue:exception.callStackReturnAddresses forKey:@"ExceptionCallStackReturnAddresses"];
[info setValue:exception.callStackSymbols forKey:@"ExceptionCallStackSymbols"];
[info setValue:exception.userInfo forKey:@"ExceptionUserInfo"];
NSError *error = [[NSError alloc] initWithDomain:@"" code:1 userInfo:info];
completion(nil, error);
}
};
// RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];
RCTAsyncLocalStorage *storage = [[RCTAsyncLocalStorage alloc] init];
dispatch_async(storage.methodQueue, ^{
@try {
// [storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
[storage performSelector:@selector(getAllKeys:callback:) withObject:rnCompletion];
}
@catch (NSException *exception) {
NSLog(@"error: %@", exception.reason);
}
});
}
当我试图得到我的一把钥匙(multiGet)
[self jsonFromLocalRNStrogeForKey:@"session" completion:^(NSDictionary* data,NSError* error) {
if (data) {
NSString * name = [data valueForKeyPath: @"token"];
if (![name isKindOfClass:[NSNull class]]) {
[self reportIncomingCallFrom:name withUUID:callInvite.uuid];
}
} else {
NSLog(@"error: JSON Parsing Error: %@",error.localizedFailureReason);
}
}];
我总是变得空虚
当我试图获取所有密钥(...@selector(getAllKeys:...)
以查看我的AsyncStorage中有什么我得到了异常
@"NSInvalidArgumentException" - reason: @"-[RCTAsyncLocalStorage getAllKeys:callback:]: unrecognized selector sent to instance 0x1085512c0"
RN在RCTAsyncLocalStorage.m中有RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)callback)
;但在RCTAsyncLocalStorage.h (void)getAllKeys:(RCTResponseSenderBlock)callback
它不存在,甚至添加它不起作用(https://github.com/facebook/react-native/blob/master/React/Modules/RCTAsyncLocalStorage.h)。
"react-native": "^0.48.4",
首先,
RN在RCTAsyncLocalStorage.m中有RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)回调);但在RCTAsyncLocalStorage.h(void)getAllKeys:(RCTResponseSenderBlock)回调它不存在
在Objective-C中,即使未使用performSelector:withObject:
在头文件中声明方法,也可以调用方法。
直接调用此方法(无需首先检查目标respondsToSelector:
)是不好的做法,因为内部方法声明可能会更改。
其次,这条线是不正确的:
[storage performSelector:@selector(getAllKeys:callback:) withObject:rnCompletion];
在这里,你说getAllKeys::
有两个参数,但是实现只声明了一个。
因此,提取所有键的正确方法如下:
dispatch_async(storage.methodQueue, ^{
if([storage respondsToSelector:@selector(getAllKeys:)]){
[storage performSelector:@selector(getAllKeys:) withObject:[^(NSArray* response){
NSLog(@"Contents: %@",response);
} copy]];
}else{
NSLog(@"storage does not respond to selector `getAllKeys:`");
}
});