首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从完成处理程序向ivar返回NSArray

从完成处理程序向ivar返回NSArray
EN

Stack Overflow用户
提问于 2014-11-11 05:13:00
回答 2查看 357关注 0票数 1

从两天开始我就有点卡住了。我正在尝试用新的Twitter-Fabric-Kit展示一个带有Twitter用户时间线的UITableView (我遵循了这个指南:https://dev.twitter.com/twitter-kit/ios/show-tweets)。我尝试这样做:通过JSON从某个用户获取Tweet- in,并将其放入一个实例变量中。这部分起作用了。问题是,数组在完成处理程序中传递给实例变量,但它似乎是异步运行的。我已经尝试用GCD来解决这个问题,但是我没有成功。也许有人可以指出正确的方向,或者甚至认为我的解决方案是垃圾,还有更好的方法;-)

代码语言:javascript
复制
#import "TweetTableViewController.h"

static NSString * const TweetTableReuseIdentifier = @"TwitterCell";

@interface TweetTableViewController ()

@property (nonatomic, strong) NSArray *tweetArray;
@property (nonatomic, strong) TWTRTweetTableViewCell *prototypeCell;

@end

@implementation TweetTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTweetArray];
//  .... tableView setup is here ...    

//    If I uncomment the next line the whole thing works!
//    NSArray *test = @[@"517330786857795584", @"516652595021352960"];

    __weak typeof(self) weakSelf = self;
    [[[Twitter sharedInstance]APIClient]loadTweetsWithIDs:self.tweetArray completion:^(NSArray *tweets, NSError *error) {
        if (tweets) {
            typeof(self) strongSelf = weakSelf;
            strongSelf.tweetArray = tweets;
            [strongSelf.tableView reloadData];
        } else {
            NSLog(@"Failed to load tweet: %@", [error localizedDescription]);
        }
    }];
}

- (void)createTweetArray {
        NSMutableArray *tempArray = [NSMutableArray array];
        [[Twitter sharedInstance]logInGuestWithCompletion:^(TWTRGuestSession *guestSession, NSError *error) {
            if (guestSession) {
                NSString *timeline = @"https://api.twitter.com/1.1/statuses/user_timeline.json";
                NSDictionary *parameters = @{@"user_id" : @"345330869"};
                NSError *clientError;
                NSURLRequest *request = [[[Twitter sharedInstance]APIClient]URLRequestWithMethod:@"GET"
                                                                                             URL:timeline
                                                                                      parameters:parameters
                                                                                           error:&clientError];
                if (request) {
                    [[[Twitter sharedInstance]APIClient]sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                        if (data) {
                            NSError *jsonError;
                            NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];
                            for (NSDictionary *dic in json) {
                                [tempArray addObject:[dic objectForKey:@"id_str"]];
                            }
                            [self handleJSONData:tempArray];
                        } else {
                            NSLog(@"Error: %@", connectionError);
                        }
                    }];
                } else {
                    NSLog(@"Error: %@", clientError);
                }
            }
        }];
}

- (void)handleJSONData:(NSMutableArray *)array {
    self.tweetArray = [NSArray arrayWithArray:array];
    NSLog(@" %@", self.tweetArray);
}

代码语言:javascript
复制
2014-11-10 22:08:41.447 [65550:3869433] numberOfSectionsInTableView
2014-11-10 22:08:41.448 [65550:3869433] numberOfSectionsInTableView
2014-11-10 22:08:41.448 [65550:3869433] numberOfRowsInSection: 0
2014-11-10 22:08:41.448 [65550:3869433] numberOfSectionsInTableView
2014-11-10 22:08:41.448 [65550:3869433] numberOfRowsInSection: 0
2014-11-10 22:08:41.449 [65550:3869433] numberOfSectionsInTableView
2014-11-10 22:08:41.450 [65550:3869433] numberOfRowsInSection: 0
2014-11-10 22:08:43.426 [65550:3869433] (
    517330786857795584,
    516652595021352960,
    514841221907243008,
    513704220600848384,
    513598624706875392,
    513598609603178496,
    513598594017132544,
    513598581471969280,
    513598566502502400,
    513229247117545472,
    511424250138611713,
    509025219584212992,
    505655616338407424,
    500357644226285568,
    474861008314322944,
    469938745438109697,
    468465457750888449,
    464706315924041728,
    464704566492418048,
    457148030731694080
)

numberOfRowsInSection和数据源都应该使用self.tweetArray数组,但是数据块似乎完成得很晚。使用静态数组,tableView可以正常工作...

谢谢你的投入,我真的被困在这里了…

EN

回答 2

Stack Overflow用户

发布于 2014-11-11 06:36:02

在handleJSONData:方法中设置断点,并验证它是否在主队列上运行。

如果它没有在主队列上运行,则不能更新UI,例如在self.tableView上调用reloadData

要在主队列上运行更新,可以使用GCD:

代码语言:javascript
复制
dispatch_async(dispatch_get_main_queue(),
^{
   [self.tableView reloadData]
});

或NSOperationQueue:

代码语言:javascript
复制
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
    [self.tableView reloadData]
}];
票数 0
EN

Stack Overflow用户

发布于 2014-11-11 06:52:44

完成处理程序总是在调用它的代码完成之后才结束。这就是异步代码的工作方式。是的,您应该在完成处理程序中重新构建为表视图的数据源提供数据的数据结构,然后调用reloadData。如果它崩溃了,那是因为bug,而不是因为这样做是错误的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26853071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档