Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOS接受到消息的方法里怎么调用不了UIViewController里的其他方法? #66

Closed
chengsan opened this issue Dec 2, 2015 · 4 comments

Comments

@chengsan
Copy link

chengsan commented Dec 2, 2015

// this function is called in a separated thread, it gets called when receive msg from icomet server
//回调接口
size_t icomet_callback(char _ptr, size_t size, size_t nmemb, void *userdata)
{
const size_t sizeInBytes = size_nmemb;
NSData data = [[NSData alloc] initWithBytes:ptr length:sizeInBytes];
NSString
s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"返回结果:%@", s);

//这里为什么调用不了UIViewController里的其他方法

return sizeInBytes;

}

@ideawu
Copy link
Owner

ideawu commented Dec 3, 2015

注释里已经说非常清楚了, 因为这是单独的线程. 你需要搜索一下iOS多线程相关的资料. 简单点说, 就是要

dispatch_async(dispatch_get_main_queue()...

@chengsan
Copy link
Author

chengsan commented Dec 3, 2015

好的,感谢

@ideawu ideawu closed this as completed Dec 3, 2015
@chengsan
Copy link
Author

chengsan commented Dec 3, 2015

能给个简单的事例吗,照你的方法好像不行
// this function is called in a separated thread, it gets called when receive msg from icomet server
//回调接口
size_t icomet_callback(char _ptr, size_t size, size_t nmemb, void *userdata)
{
const size_t sizeInBytes = size_nmemb;
NSData data = [[NSData alloc] initWithBytes:ptr length:sizeInBytes];
NSString
s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

//开启主线程
dispatch_async(dispatch_get_main_queue(), ^
               {

                   if([NSThread isMainThread])
                   {
                       NSLog(@"回到主线程");
                   }
                   [self setTextResult:s];//这句话说self没有定义

               });



return sizeInBytes;

}

@ideawu
Copy link
Owner

ideawu commented Dec 4, 2015

你好, 之前回复的方案不完整, 我已经更新了 wiki 中的示例, 你参考示例代码即可: https://github.com/ideawu/icomet/wiki/iComet-for-iOS-App

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants