这可能是一个有点幼稚的问题,但在我加载 ViewController 时,我正在使用一组方法(例如下面的 getEachItem)加载应用程序需要的所有内容。这通常是 2 或 3 个项目,它们都被写入缓存。
我想调用在 getEachItem 的最终实例完成后运行的方法“showNavigation”,但不确定如何执行此操作。 getEachItem 使用 AFNetworking 执行 GET 请求。类似于 jQuery 完整 block ,但用于下面的 for 循环的整体。
NSArray *tmpItems=[result objectForKey:@"ipad_items"];
for(NSDictionary *m in tmpItems){
// will also increment into the _menus array
[self getEachItem:[m objectForKey:@"id"]];
[self getImages:[m objectForKey:@"id"]];
}
[self showNavigation];
最佳答案
当您调用 AFNetworking GET 方法时,它会返回一个 AFHTTPRequestOperation(一个 NSOperation 子类)。您可以利用这一事实对您的问题采用基于操作队列的解决方案。也就是说,您可以创建一个新的“完成操作”,它依赖于特定 AFNetworking 操作的完成。
例如,您可以更改 getEachItem 方法以返回 GET 方法返回的 AFHTTPRequestOperation。例如,假设您有一个 getEachItem 当前定义如下:
- (void)getEachItem:(id)identifier
{
// do a lot of stuff
[self.manager GET:... parameters:... success:... failure:...];
}
将其更改为:
- (NSOperation *)getEachItem:(id)identifier
{
// do a lot of stuff
return [self.manager GET:... parameters:... success:... failure:...];
}
然后,您可以创建自己的完成操作,这将依赖于所有其他 AFHTTPRequestOperation 操作完成。因此:
NSOperation *completion = [NSBlockOperation blockOperationWithBlock:^{
[self showNavigation];
}];
NSArray *tmpItems=[result objectForKey:@"ipad_items"];
for(NSDictionary *m in tmpItems){
// will also increment into the _menus array
NSOperation *operation = [self getEachItem:[m objectForKey:@"id"]];
[completion addDependency:operation];
[self getImages:[m objectForKey:@"id"]];
}
[[NSOperationQueue mainQueue] addOperation:completion];
完成后,completion 操作将不会触发,直到所有 getEachItem 操作完成。请注意,此完成操作将在核心 AFHTTPRequestOperation 对象完成时触发,但无法保证这些请求各自的完成 block 一定会完成。
另一种方法是使用 GCD“组”。使用此技术,您在提交每个请求时“进入”该组,在 GET 方法的完成 block 中“离开”该组。然后,您可以指定当组通知您离开组的次数与进入组的次数一样多时要执行的代码块(即所有 AFNetworking 网络请求及其成功/失败 block ,完成)。
比如给getEachItem添加一个dispatch_group_t参数:
- (void)getEachItem:(id)identifier group:(dispatch_group_t)group
{
dispatch_group_enter(group);
// do a lot of stuff
[self.manager GET:... parameters:... success:^(...) {
// do you success stuff and when done, leave the group
dispatch_group_leave(group);
} failure:^(...) {
// do you failure stuff and when done, leave the group
dispatch_group_leave(group);
}];
}
请注意,您在提交请求之前“进入”组, success 和 failure block 都必须调用 dispatch_group_leave 。
完成后,您现在可以在请求循环中使用 dispatch_group_t,在组收到一切已完成的通知时执行 showNavigation:
dispatch_group_t group = dispatch_group_create();
NSArray *tmpItems=[result objectForKey:@"ipad_items"];
for(NSDictionary *m in tmpItems){
// will also increment into the _menus array
[self getEachItem:[m objectForKey:@"id"] group:group];
[self getImages:[m objectForKey:@"id"]];
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
[self showNavigation];
});
关于ios - 在 Objective-C 中是否可以在一组其他方法完成执行后调用一个方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465939/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个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=>