我有一个使用AFJSONRequestOperation的函数,我希望仅在成功后返回结果.你能指出我正确的方向吗?对于块和AFNetworking,我仍然有点无能为力.
-(id)someFunction{
__block id data;
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id json){
data = json;
return data; // won't work
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation: operation];
return data; // will return nil since the block doesn't "lock" the app.
}
Run Code Online (Sandbox Code Playgroud) 在C/Obj-C中,我们做了一个类似于此的typedef typedef int MYINT;,这很清楚.
为块执行typedef - typedef void (^MyBlock) (int a);
现在,我们可以使用MyBlock.
它不应该像 - typedef void (^MyBlock) (int a) MyBlock;类似#define吗?
语法如何工作?
我想知道是否可以将对匿名函数(块)的引用存储为Objective-C中的实例变量.
我知道如何使用委托,目标行动等.我不是在谈论这个.
在教程中,它写的功能上都是相同的,甚至闭包更容易阻塞,它避免了块和内存管理的复杂性,我已经经历了很多教程,但除了这些我没有得到swift的"闭包"和Objective-C"阻止".
任何人都可以深刻解释这种差异.任何帮助,将不胜感激.
我对使用自我内部块感到困惑,我浏览了一些Apple的文档,但仍然无法找到正确的答案.
有些人总是说在块内部使用弱自我,但有些人说在复制的块中使用弱自我,而不是总是使用.
样本1:
self.handler = ^(id response, NSError *error)
{
self.newresponse = response; //use weak self here
};
Run Code Online (Sandbox Code Playgroud)
样本2:
使用弱自我;
__weak myViewController *weakSelf = self;
[UIView animateWithDuration:interval delay:0.0 options:curve animations:^
{
[weakSelf.view.superview setTransform:CGAffineTransformMakeTranslation(0, -106)];
//in above is it use of weak is neassary
}
completion:^(BOOL finished)
{
}];
Run Code Online (Sandbox Code Playgroud)
没有弱小的自我;
__weak myViewController *weakSelf = self;
[UIView animateWithDuration:interval delay:0.0 options:curve animations:^
{
[myViewController.view.superview setTransform:CGAffineTransformMakeTranslation(0, -106)];
}
completion:^(BOOL finished)
{
}];
Run Code Online (Sandbox Code Playgroud)
在上面的样本中,哪些是正确的......?**我正在使用ARC
如果我有一个NSArray并且我enumerateUsingBlock用来遍历数组中的元素,但在某些情况下我需要跳过循环体并转到下一个元素,continue块中是否有任何等价物,或者我可以continue直接使用?
谢谢!
更新:
只是想澄清一下,我想做的是:
for (int i = 0 ; i < 10 ; i++)
{
if (i == 5)
{
continue;
}
// do something with i
}
Run Code Online (Sandbox Code Playgroud)
我需要的是continue块中的等价物.
我有一个关于iOS中块的自我强弱参考的问题.我知道在块内引用self的正确方法是在块外创建一个弱引用,然后在块内强引用该弱引用,如下所示:
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
typeof(self) strongSelf = weakSelf;
NSLog(@"%@", strongSelf.someProperty);
});
Run Code Online (Sandbox Code Playgroud)
但是,如果你有嵌套块会发生什么?一组参考文献足够吗?或者您是否需要为每个区块设置新组?例如,以下哪项是正确的?
这个:
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
typeof(self) strongSelf = weakSelf;
NSLog(@"%@", strongSelf.someProperty);
dispatch_async(dispatch_get_main_queue(), ^ {
strongSelf.view.frame = CGRectZero;
});
});
Run Code Online (Sandbox Code Playgroud)
或这个:
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^ {
typeof(self) strongSelf = weakSelf;
NSLog(@"%@", strongSelf.someProperty);
__weak typeof(strongSelf) weakSelf1 = strongSelf;
dispatch_async(dispatch_get_main_queue(), ^ {
typeof(strongSelf) strongSelf1 = weakSelf1;
strongSelf1.view.frame = CGRectZero;
});
});
Run Code Online (Sandbox Code Playgroud)
非常感谢任何信息或解释!
最近,我需要一个可用于保证在特定串行调度队列上同步执行给定块的函数.有可能从已经在该队列上运行的东西调用这个共享函数,所以我需要检查这种情况,以防止从同步调度到同一队列的死锁.
我使用如下代码来执行此操作:
void runSynchronouslyOnVideoProcessingQueue(void (^block)(void))
{
dispatch_queue_t videoProcessingQueue = [GPUImageOpenGLESContext sharedOpenGLESQueue];
if (dispatch_get_current_queue() == videoProcessingQueue)
{
block();
}
else
{
dispatch_sync(videoProcessingQueue, block);
}
}
Run Code Online (Sandbox Code Playgroud)
此函数依赖于使用此函数dispatch_get_current_queue()来确定此函数正在运行的队列的标识,并将其与目标队列进行比较.如果匹配,它知道只运行块内联而不调度到该队列,因为该函数已在其上运行.
我听说过是否适当使用dispatch_get_current_queue()这样的比较是有争议的,我在标题中看到了这个措辞:
建议仅用于调试和记录目的:
代码不得对返回的队列做任何假设,除非它是全局队列之一或代码本身创建的队列.如果该队列不是dispatch_get_current_queue()返回的队列,则代码不得假设同步执行到队列是安全的死锁.
此外,在iOS 6.0中(但尚未针对Mountain Lion),GCD标题现在将此功能标记为已弃用.
听起来我不应该以这种方式使用这个功能,但我不确定我应该在它的位置使用什么.对于像上面那样针对主队列的函数,我可以使用[NSThread isMainThread],但是如何检查我是否在我的一个自定义串行队列上运行以便防止死锁?
假设我在UIViewController子类中有以下方法:
- (void)makeAsyncNetworkCall
{
[self.networkService performAsyncNetworkCallWithCompletion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];
}
});
}];
}
Run Code Online (Sandbox Code Playgroud)
我知道self块内部的引用导致UIViewController实例被块保留.只要performAsyncNetworkCallWithCompletion不将块存储在我的属性(或ivar)中NetworkService,我是否认为没有保留周期?
我意识到上面的这个结构将导致UIViewController被保留直到performAsyncNetworkCallWithCompletion完成,即使它是由系统早先发布的.但它可能(甚至可能吗?),系统会收回我的UIViewController 所有(更改到iOS 6的一个管理方式后UIViewController的后盾CALayer内存)?
如果有理由我必须做"弱自我/强自我舞蹈",它看起来像这样:
- (void)makeAsyncNetworkCall
{
__weak typeof(self) weakSelf = self;
[self.networkService performAsyncNetworkCallWithCompletion:^{
typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.activityIndicatorView stopAnimating];
}
});
}];
}
Run Code Online (Sandbox Code Playgroud)
但是我觉得这很难看,并且如果没有必要就想避免它.
objective-c uiviewcontroller ios objective-c-blocks retain-cycle
我需要从我的视图控制器调用类方法,让它做它的事情,但然后只在类方法完成后执行一些操作.
(我认为我需要的是一个完成块,但如果我错了请纠正我.)
情况如下:
我正在使用Parse.com作为我的应用程序的后端.当用户注册帐户时,他们会在弹出窗口中输入他们的姓名,公司和其他信息,然后单击"提交".提交按钮链接到一个类方法(如下所示),该方法获取其PFUser对象和公司名称并创建一些数据库对象.函数完成后,使用委托解除弹出窗口.
问题是我需要按特定顺序创建这些对象,因为它们依赖于彼此对象的存在.问题是,在单击提交后立即调用解除弹出窗口的委托方法,因为它是堆栈中的下一个.
保存Parse对象时,会调用一个看起来像这样的方法:(这是我希望编写的,我认为可以解决我的问题)
[someParseObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// Code here runs AFTER the method completes.
// This also happens on another thread which
// I'd like to implement as well.
}];
Run Code Online (Sandbox Code Playgroud)
所以,我需要弄清楚如何做以下事情:(与块有关的一切都是完全错误的我确定)
SignUpViewController.m
myUserOrg *userOrg = [myUserOrg object]; // myUserOrg = Custom PFObject Subclass
// My method that takes in a user object and a string, creates
// the database objects in order.
[userOrg registerNewUserOrgWithUser:(PFUser*) andCompanyName:(NSString*) companyName withBlock(somethingHere)block {
if(error) {
NSLog(@"Unable to create org!"); …Run Code Online (Sandbox Code Playgroud) ios ×7
objective-c ×7
afnetworking ×1
closures ×1
cocoa ×1
cocoa-touch ×1
macos ×1
nsarray ×1
retain-cycle ×1
swift ×1
synchronous ×1
typedef ×1