我有一个用例,我使用多个数据源异步加载一些数据。因此,每个数据源都有一个完整的方法,我的 View Controller 实现了一个定义了该方法的协议(protocol)。例如,我的一个数据源是 fetchApples,它向我的 Controller 返回一个 Fruit 对象数组,另一个数据源是 fetchOranges 等。在我的 viewController 中,我想让苹果先出现,然后是橙子,然后是葡萄(我填充了一个 uiview带有用于渲染水果的自定义单元格)。如果没有苹果,橘子应该首先出现等等。当我的数据源被异步返回时,我如何映射这个顺序。 IE。当橘子回来时,我不知道我是否会有苹果,因此我还不能用它们填充 uiview?
最佳答案
使用 dispatch_group。每个服务器调用都会填充它自己的项目数组,您可以将它添加到一个主数组(数据源)或单独使用它们。
当所有服务器调用完成后,您将收到通知,然后您可以使用您想要的任何数据源以任何顺序重新加载您的表。
示例(使用 dispatch_group 和 UITableView):
//
// 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 action:@selector(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)numberOfSectionsInTableView:(UITableView *)tableView {
NSInteger sectionCount = _apples.count ? 1 : 0;
sectionCount += _oranges.count ? 1 : 0;
sectionCount += _grapes.count ? 1 : 0;
return sectionCount ? sectionCount : 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)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 *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)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)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 60.0f;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)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 *)cellForFruit:(Fruit)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 addObjectsFromArray:oranges];
}
}
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
关于ios - 异步加载数据但按顺序填充 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38416479/
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
鉴于我有以下迁移:Sequel.migrationdoupdoalter_table:usersdoadd_column:is_admin,:default=>falseend#SequelrunsaDESCRIBEtablestatement,whenthemodelisloaded.#Atthispoint,itdoesnotknowthatusershaveais_adminflag.#Soitfails.@user=User.find(:email=>"admin@fancy-startup.example")@user.is_admin=true@user.save!ende
我遵循了教程http://gettingstartedwithchef.com/,第1章。我的运行list是"run_list":["recipe[apt]","recipe[phpap]"]我的phpapRecipe默认Recipeinclude_recipe"apache2"include_recipe"build-essential"include_recipe"openssl"include_recipe"mysql::client"include_recipe"mysql::server"include_recipe"php"include_recipe"php::modul
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c