• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 异步加载数据但按顺序填充 View

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:20:20 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个用例,我使用多个数据源异步加载一些数据。因此,每个数据源都有一个完整的方法,我的 View Controller 实现了一个定义了该方法的协议(protocol)。例如,我的一个数据源是 fetchApples,它向我的 Controller 返回一个 Fruit 对象数组,另一个数据源是 fetchOranges 等。在我的 viewController 中,我希望先出现苹果,然后是橙子,然后是葡萄(我填充了一个 uiview带有用于渲染水果的自定义单元格)。如果没有苹果,橙子应该首先出现等。当我的数据源被异步返回时,我如何映射这个顺序。 IE。当橙子回来时,我不知道我是否会有苹果,因此我还不能用它们填充 uiview?



Best Answer-推荐答案


使用 dispatch_group。每个服务器调用都会填充自己的项目数组,您可以将其添加到一个主数组(数据源)或单独使用它们。

所有服务器调用完成后,您会收到通知,然后您可以使用所需的任何数据源以您想要的任何顺序重新加载您的表..

示例(使用 dispatch_groupUITableView):

//
//  ViewController.m
//  StackOverflowExample
//
//  Created by Brandon Anthony on 2016-07-16.
//  Copyright © 2016 XIO. All rights reserved.
//

#import "ViewController.h"


typedef NS_ENUM(NSInteger, Fruit) {
    Apples,
    Oranges,
    Grapes
};

#define kImageCellIdentifier @"kImageCellIdentifier"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIButton *testButton;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *apples;
@property (nonatomic, strong) NSMutableArray *oranges;
@property (nonatomic, strong) NSMutableArray *grapes;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _apples = [[NSMutableArray alloc] init];
    _oranges = [[NSMutableArray alloc] init];
    _grapes = [[NSMutableArray alloc] init];

    [self initControls];
    [self setTheme];
    [self registerClasses];
    [self doLayout];

    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)initControls {
    _testButton = [[UIButton alloc] init];
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
}

- (void)setTheme {
    [_testButton setTitle"Test Again" forState:UIControlStateNormal];
    [_testButton setBackgroundColor:[UIColor lightGrayColor]];
    [_testButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [[_testButton layer] setCornerRadius:5.0f];
    [_testButton addTarget:self actionselector(loadData) forControlEvents:UIControlEventTouchUpInside];

    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
}

- (void)registerClasses {
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kImageCellIdentifier];
}

- (void)doLayout {
    [self.view addSubview:_testButton];
    [self.view addSubview:_tableView];

    NSDictionary *views = @{@"testButton":_testButton, @"tableView":_tableView};
    NSMutableArray *constraints = [[NSMutableArray alloc] init];

    [constraints addObject:[NSString stringWithFormat"H:[testButton(%d)]-%d-|", 150, 15]];
    [constraints addObject:[NSString stringWithFormat"H:|-%d-[tableView]-%d-|", 0, 0]];
    [constraints addObject:[NSString stringWithFormat"V:|-%d-[testButton(%d)]-%d-[tableView]-%d-|", 25, 44, 10, 0]];

    for (NSString *constraint in constraints) {
        [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:constraint options:0 metrics:nil views:views]];
    }

    for (UIView *view in self.view.subviews) {
        [view setTranslatesAutoresizingMaskIntoConstraints:NO];
    }
}


- (NSInteger)numberOfSectionsInTableViewUITableView *)tableView {
    NSInteger sectionCount = _apples.count ? 1 : 0;
    sectionCount += _oranges.count ? 1 : 0;
    sectionCount += _grapes.count ? 1 : 0;
    return sectionCount ? sectionCount : 1;
}

- (NSInteger)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)section {
    if (section == 0) {
        return _apples.count ?: _oranges.count ?: _grapes.count ?: 0;
    }

    if (section == 1) {
        return _oranges.count ?: _grapes.count ?: 0;
    }

    return _grapes.count;
}

- (NSString *)tableViewUITableView *)tableView titleForHeaderInSectionNSInteger)section {
    if (section == 0) {
        return _apples.count ? @"Apples" : (_oranges.count ? @"Oranges" : (_grapes.count ? @"Grapes" : @"No Fruits"));
    }

    if (section == 1) {
        if (_apples.count) {
            return _oranges.count ? @"Oranges" : (_grapes.count ? @"Grapes" : @"No Fruits");
        }
    }

    return @"Grapes";
}

- (CGFloat)tableViewUITableView *)tableView heightForRowAtIndexPathNSIndexPath *)indexPath {
    return 60.0f;
}

- (UITableViewCell *)tableViewUITableView *)tableView cellForRowAtIndexPathNSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        Fruit fruit = _apples.count ? Apples : (_oranges.count ? Oranges : (_grapes.count ? Grapes : 0));
        return [self cellForFruit:fruit tableView:tableView indexPath:indexPath];
    }

    if (indexPath.section == 1) {
        if (_apples.count) {
            Fruit fruit = _oranges.count ? Oranges : (_grapes.count ? Grapes : 0);
            return [self cellForFruit:fruit tableView:tableView indexPath:indexPath];
        }
    }

    return [self cellForFruit:Grapes tableView:tableView indexPath:indexPath];
}

- (UITableViewCell *)cellForFruitFruit)kind tableView:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:kImageCellIdentifier forIndexPath:indexPath];

    switch (kind) {
        case Apples: {
            NSString *kind = [_apples objectAtIndex:indexPath.row];

            [[cell imageView] setImage:nil]; //Some Image..
            [[cell textLabel] setText:kind];
        }
            break;

        case Oranges: {
            NSString *kind = [_oranges objectAtIndex:indexPath.row];

            [[cell imageView] setImage:nil]; //Some Image..
            [[cell textLabel] setText:kind];
        }
            break;

        case Grapes: {
            NSString *kind = [_grapes objectAtIndex:indexPath.row];

            [[cell imageView] setImage:nil]; //Some Image..
            [[cell textLabel] setText:kind];
        }
            break;

        default:
            break;
    }
    return cell;
}

- (void)loadData {
    [_apples removeAllObjects];
    [_oranges removeAllObjects];
    [_grapes removeAllObjects];

    dispatch_group_t group = dispatch_group_create();

    dispatch_group_enter(group);
    [self getApples:^(NSArray *apples) {

        if (apples.count) {
            @synchronized (self.apples) {
                [_apples addObjectsFromArray:apples];
            }
        }

        dispatch_group_leave(group);
    }];

    dispatch_group_enter(group);
    [self getOranges:^(NSArray *oranges) {

        if (oranges.count) {
            @synchronized (self.oranges) {
                [_oranges addObjectsFromArrayranges];
            }
        }

        dispatch_group_leave(group);
    }];

    dispatch_group_enter(group);
    [self getGrapes:^(NSArray *grapes) {

        if (grapes.count) {
            @synchronized (self.grapes) {
                [_grapes addObjectsFromArray:grapes];
            }
        }

        dispatch_group_leave(group);
    }];

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{
        [self.tableView reloadData];
    });
}






//Simulating server calls..

- (void)getApples:(void(^)(NSArray *apples))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *apples = @[@"Red Apple", @"Sweet Apple", @"Sour Apple"];

        bool error = arc4random_uniform(100) <= 50;
        if (error) {
            completion(nil);
        }
        else {
            completion(apples);
        }
    });
}

- (void)getOranges:(void(^)(NSArray *oranges))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *apples = @[@"Tiny Orange", @"Bruised Orange", @"Red Orange"];

        bool error = arc4random_uniform(100) <= 50;
        if (error) {
            completion(nil);
        }
        else {
            completion(apples);
        }
    });
}

- (void)getGrapes:(void(^)(NSArray *grapes))completion {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSArray *apples = @[@"Baby Grapes", @"Green Grapes", @"I ran out of ideas for names.. Grapes"];

        bool error = arc4random_uniform(100) <= 50;
        if (error) {
            completion(nil);
        }
        else {
            completion(apples);
        }
    });
}

@end

http://i.imgur.com/S1DiqGW.png

enter image description here

关于ios - 异步加载数据但按顺序填充 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416479/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap