Search code examples
rubymotionsendbird

Rubymotion translation of SendBird SDK methods


I am working on a SendBird chat integration with Rubymotion. I am running into some problems since I am unable to get a Messaging Channel List from the SDK due to my limited knowledge of Objective-C / Swift structures.

I would appreciate some help translating the following code into RubyMotion code:

Objective-C:

- (void) queryMessagingChannels
{
    messagingChannelListQuery = [SendBird queryMessagingChannelList];
    [messagingChannelListQuery setLimit:15];
    [messagingChannelListQuery nextWithResultBlock:^(NSMutableArray *queryResult) {
        ...
        for (int i = 0; i < [queryResult count]; i++) {
            SendBirdMessagingChannel *mc = (SendBirdMessagingChannel *)[queryResult objectAtIndex:i];
            ...
        }
        ...
    } endBlock:^(NSError *error) {
    }];
}

Swift:

func queryMessagingChannels() {
    messagingChannelListQuery = SendBird.queryMessagingChannelList()
    messagingChannelListQuery?.setLimit(15)
    messagingChannelListQuery?.nextWithResultBlock({ (queryResult) -> Void in
        ....
        for model in queryResult {
            let mc: SendBirdMessagingChannel = model as! SendBirdMessagingChannel
            ....
        }
        ....
        }, endBlock: { (code) -> Void in
    })
}

So far I have this, but it is failing with an error.

Rubymotion:

mp "Iterating over MessagingChannelList"
@messagingChannelListQuery = SendBird.queryMessagingChannelList()
@messagingChannelListQuery.setLimit(5)
@messagingChannelListQuery.nextWithResultBlock.each do |queryResult|
  @messaging_channels << queryResult
end
# reload tableview data here

Error:

chat_messaging_screen.rb:49:in `load_async': undefined method `nextWithResultBlock' for #<SendBirdMessagingChannelListQuery:0x117c672e0> (NoMethodError)

I really don't know how to properly set up the block in this case.


Solution

  • Ok. So the nextWithResultBlock actually requires passing endBlock too, so this finally solved it:

    @messagingChannelListQuery.nextWithResultBlock(
      -> (queryresult) {
      #do something here with queryresult.each
      },
      endBlock: -> (code) {
      }
    )
    

    The NoMethodError was throwing me off.