首页 \ 问答 \ 等到对象可用(Wait until an object is available)

等到对象可用(Wait until an object is available)

我相信我的用例相当普遍,但是我找不到任何可以让我100%确定我在做什么的文档。 任何指针都很受欢迎。

在我的应用程序中的某个时刻,我开始下载一个对象。 之后,用户可以单击按钮。 如果对象完成下载,我想执行一些代码。 否则我想等到下载对象并执行相同的代码。 如果用户没有点击按钮,我不想做任何事情。 下载的对象将丢失。

我的基本想法是做这样的事情:

NSObject *myObj = nil;

- (void)download {
  [self downloadObj:^(NSObject *obj){
    myObj = obj;
  }];
}

- (void)buttonClicked {
  waitOrExecuteDirectly:^{
    // Some code with myObj
  }
}

当然,第一个问题是“我该如何等待?”

所以我尝试了

- (void)buttonClicked {
  if(myObj) {
    // Some code
  } else {
    // Wait then do the exact same code
  }
}

但我认为棘手的问题是“如果对象在”if“计算之后和”else“块进入之前完成下载会发生什么?”。

我试图将下载封装在NSOperation并使用completionBlock属性。 但是如果在设置回调时操作已经完成,则永远不会调用completionBlock。 我不想在“下载”方法中设置回调,因为用户可能没有点击按钮。

是否有内置机制允许我对将根据任务状态等待或直接执行的任务提供完成回调? 如果没有,那么我自己做的最佳做法是什么? 设置和读取myObj时使用NSLock


I believe my use case is fairly common, but I couldn't find any documentation that would make me 100% sure about what I am doing. Any pointer is appreciated.

At some point in my app, I begin to download an object. Afterwards, the user can click a button. If the object is done downloading, I want to execute some code. Otherwise I want to wait until the object is downloaded and execute the same piece of code. If the user does not click the button, I don't want to do anything. The downloaded object is lost.

My basic idea was to do something like this:

NSObject *myObj = nil;

- (void)download {
  [self downloadObj:^(NSObject *obj){
    myObj = obj;
  }];
}

- (void)buttonClicked {
  waitOrExecuteDirectly:^{
    // Some code with myObj
  }
}

Of course, the first problem is "how do I wait?"

So I tried with

- (void)buttonClicked {
  if(myObj) {
    // Some code
  } else {
    // Wait then do the exact same code
  }
}

But I think the trickier problem is "what happens if the objects finishes downloading right after the "if" is calculated and before the "else" block is entered?".

I tried to encapsulate the download within an NSOperation and use the completionBlock property. But if the operation has already finished when I set the callback, the completionBlock is never called. I do not want to set the callback in the "download" method because the user might not click on the button.

Is there a built-in mechanism that allows me to give a completion callback to a task that will wait or execute directly depending on the task status? If not, what would be the best practice to do it by myself? Use a NSLock when setting and reading myObj?


原文:https://stackoverflow.com/questions/19806694
更新时间:2023-06-27 20:06

最满意答案

尝试这个:

$('nav ul ul.dropdown-menu').removeClass('row fluid-container').addClass('container');
$(window).on('resize',function(){
  $('nav ul ul.dropdown-menu').css({padding:$('.container').css('margin')})
});

http://jsfiddle.net/7jkmfrjz/1/


Try this:

$('nav ul ul.dropdown-menu').removeClass('row fluid-container').addClass('container');
$(window).on('resize',function(){
  $('nav ul ul.dropdown-menu').css({padding:$('.container').css('margin')})
});

http://jsfiddle.net/7jkmfrjz/1/

相关问答

更多