Objective-c块同步数据

时间:2015-05-08 22:05:02

标签: objective-c

我正在开发一个从Instagram获取资源的程序。我对objective-c真的很陌生,所以请耐心等待我的问题。我有一个调用方法来获取提要的按钮,但它是一个块,所以当方法完成时,有时块仍然继续加载图像。更改用户时出现问题。我清除了媒体数组,但触发的方法用旧用户数据填充数组。我试图使用userchanged属性,但它似乎也取消了当前的用户数据。我该如何同步这些方法?我无法弄清楚。

  

@property(非原子,复制)NSMutableArray * mediaArray;

     

@property(nonatomic,assign)BOOL currentUserChanged;

- (void)getInstagramSelfFeed
{        
    if (self.currentPaginationInfo.nextMaxId != self.requestedPaginationInfo)
    {   
        self.requestedPaginationInfo = self.currentPaginationInfo.nextMaxId;

        [[InstagramEngine sharedEngine] getSelfFeedWithCount:15 maxId:self.currentPaginationInfo.nextMaxId success:^(NSArray *media, InstagramPaginationInfo *paginationInfo){

            self.currentPaginationInfo = paginationInfo;

            if ([self checkIfCurrentUserChanged])
            {
                return;
            }

            [self.mediaArray addObjectsFromArray:media];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"finishGettingSelfFeed" object:self];

        } failure:^(NSError *error) {
            NSLog(@"Request Self Feed Failed %@", error);
        }];
    }
}

- (void)setCurrentUser:(User *)currentUser
{
    if (_currentUser != currentUser)
    {
        _currentUser = currentUser;

        [[InstagramEngine sharedEngine] setAccessToken:self.currentUser.accessToken];

        [self.mediaArray removeAllObjects];

        if ([self.currentViewController isKindOfClass:[TimelineViewController class]])
        {
            self.currentUserChanged = YES;
        }
        MenuController *menuController = [MenuController sharedManager];

        [menuController updateAllViews];
    }
}

- (User *)currentUser
{
    return _currentUser;
}

- (BOOL)checkIfCurrentUserChanged
{
    if (self.currentUserChanged)
    {
        self.currentUserChanged = false;

        return YES;
    }
    return NO;
}

2 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题。现在我发送发出请求的用户并比较两者。

- (void)getInstagramSelfFeed: (User *)user
{
    if (self.currentPaginationInfo.nextMaxId != self.requestedPaginationInfo)
    {   
        self.requestedPaginationInfo = self.currentPaginationInfo.nextMaxId;

        self.executingGetSelfFeed = true;

        [[InstagramEngine sharedEngine] getSelfFeedWithCount:15 maxId:self.currentPaginationInfo.nextMaxId success:^(NSArray *media, InstagramPaginationInfo *paginationInfo){

            self.currentPaginationInfo = paginationInfo;

            if (user != self.currentUser)
            {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"gettingSelfFeedCancelled" object:self];
                return;
            }

            [self.mediaArray addObjectsFromArray:media];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"finishGettingSelfFeed" object:self];

        } failure:^(NSError *error) {
            NSLog(@"Request Self Feed Failed %@", error);
        }];
    }
}

答案 1 :(得分:0)

一个很大的问题是你有一个可变数组,它是一个"副本"属性。这是错的。当您访问self.mediaArray时,会生成实例变量的副本,然后您可以修改该副本。实例变量本身永远不会被修改。