UA-17470720-3

Jump to content


Photo
- - - - -

reference SELF


  • Please log in to reply
4 replies to this topic

#1 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 19 January 2016 - 03:30 AM

I have sooooooooooo much code with weakSelf everywhere

 

__weak typeof(self) weakSelf = self;
selectionSection.sectionActions.customPresentDetailModel = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath) {
    if (weakSelf) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf doThis];
        [strongSelf doThat];
    }
});

 

is there anyway possible to use STV where we can set  (or have automatically set for us) in the tableViewModel a reference to SELF??

 

selectionSection.sectionActions.customPresentDetailModel = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath) {
    [section.ownerTableViewModel.self doThis];
    [section.ownerTableViewModel.self doThat];
});

 

were a weak SELF is automatically available for us?

 

Oz


P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 19 January 2016 - 05:39 AM

Hi ozie,

 

You're not alone! I always thought a good implementation by Apple would've been to have the compiler automatically replace 'self' with a version of weak_self inside blocks, unless specifically marked otherwise by the developer (kind of like how ARC automatically inserts retain/release). In this scenario, if the developer makes a mistake in correctly marking 'self', the code would just crash, instead of just silently leaking memory as is the case now.

 

Having said that, I really don't understand what you're doing with 'strongSelf' in your code. You don't need that at all, just use '[weak_self doThis]'.

 

As for your idea, you could already do that with the existing STV! Let's say your view controller's class is 'RootViewController'. Your code can become:

 

selectionSection.sectionActions.customPresentDetailModel = ^(SCTableViewSection *section, SCTableViewModel *detailModel, NSIndexPath *indexPath) {
    [(RootViewController *)section.ownerTableViewModel.viewController doThis];
    [(RootViewController *)section.ownerTableViewModel.viewController doThat];
});

 

The above is of course perfectly safe and doesn't cause retain loops or memory leaks. Hope this helps.


  • Dave Guerin, ozie and wizgod like this

#3 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 19 January 2016 - 02:40 PM

for the strongSelf

according to Clang’s documentation it’s possible that weakSelf can get automatically nilled after [weakSelf doThis]—before [weakSelf doThat] is ever called—because Clang may decide to re-evaluate object retention on a statement-by-statement basis

 

OOOOooooo.. i do like the thought of [(RootViewController *)section.ownerTableViewModel.viewController doThis];

 

will have to take a few minutes to turn that into a simplified #define where i can pass in just the model object .. just to cut down on typing :)


  • Dave Guerin likes this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#4 ozie

ozie

    ¯\_(ツ)_/¯

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 526 posts
  • LocationAustralia
Reputation: 169
Popular

Posted 19 January 2016 - 04:32 PM

#define SELFPERFORM(model,selector) ((void (*)(id, SEL))[model.viewController methodForSelector:selector])(model.viewController, selector);

 

that works well for quick things

 

but thanks to DASH i also created a snip for "ws" where is creates the line

RootViewController *weakSelf = (RootViewController *)section.ownerTableViewModel.viewController;

 

for easy of reuse and more complex tasks

 

:)


  • Tarek and Dave Guerin like this

P.S. I hate Swift.. don't talk Swift.. Too old school to learn yet another programming language.


#5 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 19 January 2016 - 08:16 PM

Thanks for sharing ozie :)






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users