googlesamples / ios-nearby

68 stars 30 forks source link

can't pub/sub when using nearby api for iOS in a cocos2dx based project #5

Closed Huang-Liangjin closed 8 years ago

Huang-Liangjin commented 8 years ago

I want to use nearby api in a cocos2dx based project (c++ only), so I made a simple wrapper for nearby API, here it is: https://github.com/Huang-Liangjin/NearbyForCPP When I call the methods below:

    spice::nearby::Nearby::getInstance()->setupForiOS();
...
    spice::nearby::Nearby::getInstance()->subscribe();

the log output is

2016-02-19 14:51:48.769 SpiceExample-mobile[430:53625] Subscribe
2016-02-19 14:51:48.787 SpiceExample-mobile[430:53625] Unubscribe
2016-02-19 14:51:48.792 SpiceExample-mobile[430:53625] Tokens: 0 good, 0 bad, 0 broadcast
2016-02-19 14:51:48.793 SpiceExample-mobile[430:53625] Report RPC request: 0 token(s), 0 beacon(s), 0 directive(s)
2016-02-19 14:51:48.803 SpiceExample-mobile[430:53625] Report RPC request: Subscribe: 1
2016-02-19 14:51:48.803 SpiceExample-mobile[430:53625] Report RPC request: Unsubscribe: 1
2016-02-19 14:51:49.651 SpiceExample-mobile[430:53625] Report RPC response: Success
2016-02-19 14:51:49.652 SpiceExample-mobile[430:53625] Report RPC response: 6 directive(s), 0 token(s), 0 message(s)
2016-02-19 14:51:49.682 SpiceExample-mobile[430:53625] Tokens: 0 good, 0 bad, 0 broadcast
2016-02-19 14:51:49.682 SpiceExample-mobile[430:53625] Report RPC request: 0 token(s), 0 beacon(s), 0 directive(s)
2016-02-19 14:51:50.064 SpiceExample-mobile[430:53625] Report RPC response: Success
2016-02-19 14:51:50.065 SpiceExample-mobile[430:53625] Report RPC response: 0 directive(s), 0 token(s), 0 message(s)

It is Unubscribed immediately after subscription.

could you help me to figure out why this happened, please?

dan-webb commented 8 years ago

The only way for that to happen is if the subscription object is deallocated. Can you double check that unsubscribe() isn't being called?

Another way the subscription could be deallocated is if its owner (the NearbyMessage object) is deallocated, but since it's a singleton, that's probably not happening.

Huang-Liangjin commented 8 years ago

Thanks very much for replying. I'm sure that I didn't call the unsubscribe() method. The same thing also happens when I call the publish(). (It is unpublished immediately after publish() is called.) Still thank you for the hint, I'll check out whether the objc singleton object get wrong or not. And I'm sorry I made an issue which would probably has nothing wrong with Nearby API itself, is it better to close it?

dan-webb commented 8 years ago

One more idea: Do you have ARC enabled for your code? If not, the publication or subscription object would be immediately deallocated.

Don't close the issue yet. I'm curious to find out what's causing the problem; I'm always looking for ways to improve the developer experience.

Huang-Liangjin commented 8 years ago

Thank you so much!!!!!!! After adding the -fobjc-arc flag to mm files, it works!!!!!!!!

dan-webb commented 8 years ago

Great! Glad to help.

Huang-Liangjin commented 8 years ago

Since you are looking for ways to improve the developer experience, I get some other suggestions would be helpful to you. After comparing the Android API and iOS API, I noticed that iOS can not set the TTL of messages, it would be nice if the TTL is configurable for iOS. :smile:

Huang-Liangjin commented 8 years ago

may I close this issue now~? :smile:

dan-webb commented 8 years ago

Thanks for the feedback!

The reasons we don't support TTL on iOS is that (a) your app will almost certainly need to update the UI when the TTL expires, so you'll need to set a timer in your app, and (b) it's dead simple to set a timer on iOS using GCD.

Here's an example that nils out your subscription after 5 minutes:

dispatch_after( dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * 60 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self unsubscribe]; });

Huang-Liangjin commented 8 years ago

Thank you for your help sincerely~ :smile: