-4

Here's atomic/nonatiomic properties:

@property (nonatomic,strong) NSString* firstName;
@property (atomic,strong) NSString* lastName;

With setters/getters:

-(void)setFirstName:(NSString *)fname{
    NSLog(@"set self.firstname: %@ ",fname);
    self.firstName = [fname uppercaseString];
}

-(NSString *)firstName{
    NSLog(@" get self.firstname: %@ ",self.firstName);
    return self.firstName;
}

-(void)setLastName:(NSString *)lName{
    NSLog(@"set self.laststname: %@ ",lName);
    self.lastName = lName.uppercaseString;
}

-(NSString *)lastName{
    NSLog(@" get self.lastname: %@ ",self.firstName);
    return self.lastName;
}

When trying to modify/acces from asynchronous queued block, it's crashed:

-(void)viewWillAppear:(BOOL)animated
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        self.firstName = @"f1";
        self.lastName = @"l1";
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        self.firstName = @"f2";
        self.lastName = @"l3";
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
        self.firstName = @"f3";
        self.lastName = @"l3";
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND,0),^{
      NSLog(@"first : %@ \t last : %@",self.firstName,self.lastName);
    });
}

@end

With the mentioned code, the questions are:

  1. Why its crashing.
  2. What the difference between atomic/nonatomic properties when dealing with multiple threads
damu
  • 3
  • 5
  • 5
    Really difficult to tell what's being asked here. Do you only want to know about atomic/nonatomic? or is the question somehow linked to your code. – Shamas S Sep 07 '15 at 19:02
  • I am using atomic and nonatomic variables .. i want to know what is difference while using multiple threads – damu Sep 07 '15 at 19:05
  • [here](http://stackoverflow.com/a/589392/2109067) is the answer – ankhzet Sep 07 '15 at 19:14
  • 1
    You sure it isn't [here](https://stackoverflow.com/help/how-to-ask)? – CaptJak Sep 07 '15 at 19:16
  • from the iOS tag wiki: http://www.raywenderlich.com/10209/my-app-crashed-now-what-part-1 . recommended reading for every ios developer! – Michael Sep 07 '15 at 19:28
  • Please edit your question to either ask about the differences or ask why your code is not working. If you need, make two questions. Helps keep this place clean. Though both of your answers are posted in the comments / Related Questions – Henry F Sep 07 '15 at 20:49

1 Answers1

0

Of course it is crashing. Look at what you are doing: You implement the setter, and the setter calls the setter again. Which calls the setter again, and so on and so on. This has nothing to do with atomic / nonatomic and nothing to do with how you call it. Any call do your setters or getters will crash because they call themselves in unlimited recursion.

BTW. This is careless:

NSLog(@" get self.lastname: %@ ",self.firstName);

BTW. The meaning of atomic / nonatomic properties is discussed in many questions.

gnasher729
  • 47,695
  • 5
  • 65
  • 91
  • okay, Thanks @gnasher729. can u explain what is the practical difference between atomic and non-atomic so may people explained in terms of description, but i want to know practically what happens in the case of atomic and non-atomic cases on multithreaded application, and what is thread safe.. if u explain with code its very helpful. Thank you. – damu Sep 08 '15 at 05:20