本文介绍了使用 RCTAsyncLocalStorage + getAllKeys的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 iOS 本机代码上获取 AsyncStorage.所以这是我的代码

I'm trying to get the AsyncStorage on iOS native code. So this is my code

- (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)

When I try to get one of my keys (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 中有什么时,我得到了异常

And when I try to get all the keys (...@selector(getAllKeys:...) to see what do I have in my AsyncStorage I got the exception

@"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/模块/RCTAsyncLocalStorage.h).

The RN have RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)callback) in RCTAsyncLocalStorage.m; but at RCTAsyncLocalStorage.h (void)getAllKeys:(RCTResponseSenderBlock)callback it doesn't exists and even adding it doesn't work (https://github.com/facebook/react-native/blob/master/React/Modules/RCTAsyncLocalStorage.h).

"react-native": "^0.48.4",

如何返回 NSJsonSerialization

推荐答案

首先,

RN 有RCT_EXPORT_METHOD(getAllKeys:(RCTResponseSenderBlock)callback) 在RCTAsyncLocalStorage.m;但在 RCTAsyncLocalStorage.h(void)getAllKeys:(RCTResponseSenderBlock)callback 它不存在

在 Objective-C 中,您可以使用 performSelector:withObject: 调用一个方法,即使它没有在头文件中声明.

In Objective-C, you can invoke a method even though it is not declared in the header file using performSelector:withObject:.

直接调用这个方法(没有首先检查目标respondsToSelector:)是不好的做法,因为内部方法声明可能会改变.

Invoking this method directly (without first checking if the target respondsToSelector:) is bad practice, as the internal method declaration may change.

其次,这一行是不正确的:

Secondly, this line is incorrect:

[storage performSelector:@selector(getAllKeys:callback:) withObject:rnCompletion];

在这里,您是说 getAllKeys:: 接受两个参数,但是实现只声明了一个.

Here, you're saying getAllKeys:: takes two arguments, however the implementation declares only one.

因此,提取所有密钥的正确方法如下:

Hence, the correct way to extract all keys is the following:

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:`");
    }

});

这篇关于使用 RCTAsyncLocalStorage + getAllKeys的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 22:29