I consider the whole of Objective-C harmful. It lives only in the 'platform' layer of my code. Grudgingly because Apple enforce it. It could be worse... they could have pulled a Google and used Java - then held back the tools they develop e.g. the Java VM because they are 'dangerous' and suggest that using native code is 'bad' if it is just for performance or cross-platform reasons. At least the interoperability with…
NSNotificationCenter with blocks considered harmful
31–39 of 39 posts
Re: NSNotificationCenter with blocks considered harmful
#32The book Programming iOS 6 by Matt Neuburg recommends the "weak-strong dance" for this, like so (p. 326): __weak MyClass* wself = self; self->observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"heyho" object:nil queue:nil usingBlock:^(NSNotification *n) { MyClass *sself = wself; if (sself) { NSLog(@"%@", sself); } }]; Do other people agree this fixes the problem? Also, it's my understanding closures…
__strong MyClass *strongSelf = weakSelf;
if(!strongSelf) return;
... Rest of block
It's also worth noting calling functions on weakSelf can in turn access ivars. Getting a strong reference to self solves both the issues. NSLog calls [self description] in your example.
Re: NSNotificationCenter with blocks considered harmful
#33The book Programming iOS 6 by Matt Neuburg recommends the "weak-strong dance" for this, like so (p. 326): __weak MyClass* wself = self; self->observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"heyho" object:nil queue:nil usingBlock:^(NSNotification *n) { MyClass *sself = wself; if (sself) { NSLog(@"%@", sself); } }]; Do other people agree this fixes the problem? Also, it's my understanding closures…
Re: NSNotificationCenter with blocks considered harmful
#34Re: NSNotificationCenter with blocks considered harmful
#35Earlier quoted context omitted.
Apple's sample code is terrible. I've always assumed it is written by interns because it is generally bug ridden and often not the right way to do something.
Do you mean the code examples in articles or the projects that you can download? Because I haven't looked at their downloadable samples in quite some time, but their docs always seem alright to me.
Re: NSNotificationCenter with blocks considered harmful
#36There are three related mistakes contributing to this bug: 1. The test is depending on dealloc being done eagerly. 2. The Attempt class puts unsubscribe code in its dealloc method. 3. The Attempt class does not force callers to control its lifetime. I'll go into more depth on these. 1. Cleanup is often deferred. Dealloc may only run AFTER the test instead of DURING the loop (e.g. I think NSArray defers releasing its…
Re: NSNotificationCenter with blocks considered harmful
#37Earlier quoted context omitted.
Apple's sample code is terrible. I've always assumed it is written by interns because it is generally bug ridden and often not the right way to do something.
Do you mean the code examples in articles or the projects that you can download? Because I haven't looked at their downloadable samples in quite some time, but their docs always seem alright to me.
Re: NSNotificationCenter with blocks considered harmful
#38I consider the whole of Objective-C harmful. It lives only in the 'platform' layer of my code. Grudgingly because Apple enforce it. It could be worse... they could have pulled a Google and used Java - then held back the tools they develop e.g. the Java VM because they are 'dangerous' and suggest that using native code is 'bad' if it is just for performance or cross-platform reasons. At least the interoperability with…
Are you saying you'd rather write apps in C++? I don't think the tradeoff is worth it.
i also don't like the ref counting. when given a choice i would rather do my own allocations.
Re: NSNotificationCenter with blocks considered harmful
#39Earlier quoted context omitted.
Any explanation for the downvotes? I concede the value of rapid application development, but objective-c itself doesn't provide this at all - rather the libraries like UIKit or Cocoa coupled with the Xcode development environment do this. Objective-C is measurably bad for my code. I have reams of data to support this... and its all trivially reproducable.
I didn't downvote you, but you directed a lot of loosely-connected negative assertions at a single target without really backing any of them up. Whatever you intended, it came across as a bit of a rant.
Objective-C is nice, but its slow by design, and this rules it out in a concrete way for a lot of the work I've done.
Its sad when optimisation comes down to 'knowing better than the compiler' only because of language constraints. (even C has this problem with struct layout for example - its just prevalent when using Objective-C/Cocoa (Touch))