0

First, I know nothing about Objective-C.

That said the follow code should be getting the data from AsyncStorage.

I've already did something like for Android where the basic idea is get the data from AsyncStorage as Json Object.

What I need now, is use the jsonFromLocalRNStrogeForKey inside callInviteReceived

jsonFromLocalRNStrogeForKey source

+(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];
                }
            }
        }

        NSData *jsonAsData = [jsonAsString dataUsingEncoding:NSUTF8StringEncoding];

        NSError *error;

        NSDictionary *json = [
            NSJSONSerialization
            JSONObjectWithData:jsonAsData
            options:NSJSONReadingMutableContainers
            error:&error
        ];

        completion(json, error);
    };

    RCTAsyncLocalStorage *storage = [RCTAsyncLocalStorage new];   

    dispatch_async(storage.methodQueue, ^{
        [storage performSelector:@selector(multiGet:callback:) withObject:@[key] withObject:rnCompletion];
    });
}

callInviteReceived

- (void)callInviteReceived:(TVOCallInvite *)callInvite {    
    NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];
    // json.user.name
    [self reportIncomingCallFrom:@json.user.name withUUID:callInvite.uuid];
}

These methods should be working something like

-(NSJsonSerialization)jsonFromLocalRNStrogeForKey: {
    ...
    return json;
}

- (void)callInviteReceived {    
    ...

    NSJsonSerialization json = [self.jsonFromLocalRNStrogeForKey];

    // json.user.name
    [self reportIncomingCallFrom:json.user.name withUUID:callInvite.uuid];
}

So, is there anyone who could show me, how to code this?


Using RCTAsyncLocalStorage + getAllKeys

Pablo
  • 1,953
  • 4
  • 28
  • 57

1 Answers1

0

The code would look something like this (You will need to supply the storageKey parameter):

- (void)callInviteReceived:(TVOCallInvite *)callInvite {    
    [self jsonFromLocalRNStrogeForKey:/*storageKey*/ completion:^(NSDictionary* data,NSError* error){
        if(data){
            NSString * name = [data valueForKeyPath@"user.name"];
            if(![name isKindOfClass:[NSNull class]]){
                [self reportIncomingCallFrom:name withUUID:callInvite.uuid];
            }
        }else{
            //handle error
            NSLog(@"JSON Parsing Error: %@",error.localizedDescription)
        }
   }
}

NSJSONSerialization is a utility class responsible for serializing and deserializing JSON. All of its methods are class methods, so it's pointless to pass it as a return value.

The output of deserialization is an NSDictionary which is a Map representation of the JSON. You can use this Map to extract values of user.name.

W.K.S
  • 9,787
  • 15
  • 75
  • 122
  • That worked! Tks a lot! By the way do you have any idea if there's an specific ```storageKey``` as root? Because every try I'm getting null. I also tried ```getAllKeys``` but I doesn't exists. – Pablo Jan 15 '18 at 19:28
  • Please update your question to include the code that you used to `getAllKeys`. `getAllKeys` is a method with a `RCTResponseSenderBlock `. Use `NSLog` to print the contents of the array that is returned in the `RCTResponseSenderBlock`. https://github.com/gpbl/react-native-blur-example/blob/master/node_modules/react-native/React/Modules/RCTAsyncLocalStorage.h#L29 – W.K.S Jan 15 '18 at 19:46
  • Sure, just did! Thank you again. – Pablo Jan 16 '18 at 10:28
  • You are calling `getAllKeys` incorrectly. Please post this as a separate question to avoid messiness. You can paste the link of that question here and I will answer it. Once `getAllKeys` works, you'll have a list of keys, and you can use the appropriate key in `jsonFromLocalRNStrogeForKey:completion` – W.K.S Jan 16 '18 at 10:33
  • Here it`s https://stackoverflow.com/questions/48280006/using-rctasynclocalstorage-getallkeys – Pablo Jan 16 '18 at 11:18