objective-c-blocks

blocks, self, retain cycles

二次信任 提交于 2020-01-22 06:01:05
问题 I'm having a bit of trouble getting my head around referencing self within a block, and not creating a retain cycle. Can you let me know if my understanding is correct: If I ever reference self within a block, it will create a retain cycle, and instead I should be creating a weak reference to self outside of the block and then using that weak reference inside the block? Thanks! 回答1: Yes, that is correct, with a few exceptions: A retain cycle only happens if self ends up retaining the block

Cannot AddObject to NSMutableArray from Block

拈花ヽ惹草 提交于 2020-01-22 01:59:05
问题 I have a feeling that my problem here is really with blocking, but maybe it's something else too. I am trying to forward geocode an address and place the coordinates into an array to use later. An exception is raised down at the bottom when I try to call on one of the objects I tried added to the array in the block. The exception also gets raised before any of the NSLogs ever print out within the block text. What's the proper way to handle this? Thanks. - (NSMutableArray *

Wait until all iOS blocks are executed before moving on

假装没事ソ 提交于 2020-01-22 01:48:25
问题 I have a data model/store object that interfaces with the Internet over several APIs containing data. The number of APIs to be interfaced with is dynamic: from a conceptual standpoint, we can consider the endpoints to be strings in an NSMutableArray. The issue is that I want to notify views/other observers of updated data after the last endpoint/API call is completed. I tried GCD dispatch but the following pattern doesn't seem to work properly: dispatch_queue_t queue = dispatch_get_global

EXC_BAD_ACCESS when copying or retaining Block

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-19 05:20:07
问题 As far as I understand a Block acts like an object, in that you can send copy or release messages to it, e.g: [myBlock copy]; However whenever I do this, or release a block, I get EXC_BAD_ACCESS. If I use the block functions, everything works as expected, e.g.: Block_copy(myBlock); I thought both ways of releasing and copying blocks were identical? It's not that much of a problem, but it is a little annoying that if I have a property (copy) which is a Block, I have to write the setter method

Using weak self in dispatch_async function

只愿长相守 提交于 2020-01-18 11:08:08
问题 I read a lot of posts about using __weak self inside dispatch_async , and now I am a litle bit confused. if I have : self.myQueue = dispatch_queue_create("com.biview.core_data", NULL); dispatch_async(self.myQueue, ^(void){ if (!self.var1) { self.var1 = ...; } dispatch_async(dispatch_get_main_queue(), ^(void) { if ([self.var2 superview]) { [self.var2 removeFromSuperview]; } [self.Label setText:text]; }); }); do I need to use __weak self . Because I read that in some cases dispatch_async not

How to avoid nested blocks

只愿长相守 提交于 2020-01-16 06:41:03
问题 GCD and blocks are so nice and convenient. But when I fall in love with it, I found that something bad happened. Look at these codes below: [self functionA:^(BOOL success) { if (success) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self functionB:^(NSError *error) { if (error != nil) { dispatch_async(dispatch_get_main_queue(), ^(void) { [self functionC:^(id result) { if (result) { [self functionD:^(BOOL success) { if (success) { [self DoSomething]

How to avoid nested blocks

雨燕双飞 提交于 2020-01-16 06:39:32
问题 GCD and blocks are so nice and convenient. But when I fall in love with it, I found that something bad happened. Look at these codes below: [self functionA:^(BOOL success) { if (success) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { [self functionB:^(NSError *error) { if (error != nil) { dispatch_async(dispatch_get_main_queue(), ^(void) { [self functionC:^(id result) { if (result) { [self functionD:^(BOOL success) { if (success) { [self DoSomething]

Blink hidden and using blocks

ⅰ亾dé卋堺 提交于 2020-01-14 05:20:07
问题 I have the method: - (void)blinkView:(UIView *)view { view.layer.opacity = 0.0f; view.hidden = NO; [UIView beginAnimations:@"Blinking" context:nil]; [UIView setAnimationRepeatCount:1.0]; [UIView setAnimationDuration:0.6f]; [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; view.layer.opacity = 1.0f; [UIView commitAnimations]; } How can i write this code with blocks, and how i must implement method with reverse effect (hide uiview with blink) ? 回答1: [UIView transitionWithView: view

about iOS target version and SDK version

依然范特西╮ 提交于 2020-01-14 03:12:25
问题 iOS SDK is upgrading, and some new tech will appear, i.e, block is new tech. So question is if "block" based implementation also can be distributed to lower target version or older iPhone ( 3G or first generation ) ? how to cope with those issues ? 回答1: The usual way to deal with this sort of issue is to selectively enable features at runtime based on the current OS version. However, this can be very complicated to manage. Query the current OS version at runtime Use weak linking Dynamically

How to make a function call in ios to wait, till the block inside that function is executed completely?

落花浮王杯 提交于 2020-01-13 01:41:11
问题 Inside the following function, I've used a block. But when I call this function, it is returned even before the block is executed. I understood that Block inturn uses the threads and executes separately so that the function doesnt wait for it to return. But, Is there any other way I could make the function execution wait, or any other way to achieve the functionality of this block without using the block itself ? -(int)findCurrentZip { CLLocation *userLocation = [[CLLocation alloc]