1

I am trying to build a flutter plugin for the Sendbird iOS SDK. For some reason whenever I try to fetch data, I get null from a NSDictionary<NSString *,NSObject *> when I know for a fact that it is not (I have created the keys and values separately).

Most likely it's the fact that being a beginner with Swift I am doing something wrong. This is my code:

 let channelMetadata = NSMutableDictionary()
  
// metadata is the NSDictionary<NSString *,NSObject *>

        channel.getAllMetaData{ (metaData, error) in
             guard let metadata = metaData, error == nil else { 
  // Error.
                        return
                     } 

            channelMetadata["status"] = metadata["status"]
            channelMetadata["type"] = metadata["type"]                  
         }
        
          js["status"] = channelMetadata["status"]

it's:

js["status"]

Which returns null. I assume this is because I am doing something wrong with the way I am populating my empty dictionary, but I don't know what, even after searching for hours. Any help would be really appreciated.

DrupalNewbie
  • 29
  • 1
  • 4
  • 1
    `getAllMetaData` is asynchronous, the closure is executed after the last line. And don't use `NSMutable...` collection types in Swift at all. – vadian Aug 24 '20 at 17:43
  • @vadian thanks for your reply. Do you suggest I use ```[String: String]()``` instead of ```NSMutableDictionary```. Should my code look like this instead: ```channel.getAllMetaData(completionHandler: { (metaData, error) in guard ...channelMetadata["status"] = metadata["status"] channelMetadata["type"] =metadata["type"] js["status"] = channelMetadata["status"] })```. Sorry as I said I am a complete newbie – DrupalNewbie Aug 24 '20 at 18:05
  • or @vadian do you mean I should use ```DispatchGroup() enter, leave``` etc? – DrupalNewbie Aug 24 '20 at 18:30
  • No, do the things you have to do inside the closure, the scope of the `{}` after `getAllMetaData` – vadian Aug 24 '20 at 18:31
  • Thanks @vadian. I guess I should rephrase my question then. What I essentially want to do it wait for `metadata` to have a value and then use that value outside the closure, sort of like `async await`, as the snippet is part of a larger function – DrupalNewbie Aug 25 '20 at 06:06
  • Don't wait, never wait. As I said move the code you need to execute into the closure or create a method with a completion handler. You need to understand (and handle efficiently) asynchronous data processing. – vadian Aug 25 '20 at 06:33
  • @vadian Thanks again. I have tried moving ```js["status"] = channelMetadata["status"]``` inside the closure, but ```js``` is a parameter in a static function that the code snippet above forms part of ```static func extractChannel( channel: SBDBaseChannel, js: inout NSMutableDictionary```. So when I do that I get an error ```error: escaping closure captures 'inout' parameter 'js'``` – DrupalNewbie Aug 25 '20 at 07:48
  • There's a new Sendbird Flutter plugin you can use, or reference if you're still building your own plugin @DrupalNewbie: https://pub.dev/packages/sendbird_sdk – Jalakoo Apr 23 '21 at 17:20

0 Answers0