Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storyboard viewcontroller circular dependency issue #307

Closed
foulkesjohn opened this issue Jan 16, 2015 · 13 comments
Closed

Storyboard viewcontroller circular dependency issue #307

foulkesjohn opened this issue Jan 16, 2015 · 13 comments

Comments

@foulkesjohn
Copy link

I am loading view controllers from TyphoonStoryboard and have a definition for the class which works fine, several properties are injected with no problems. However one of the properties is a view which itself has a delegate property which I want to be the view controller instance. When the view comes to set the delegate property it tries to create a new instance of the view controller instead of using the instance which has been created by the storyboard. How can I make the delegate property use the current instance of the view controller? My definitions look something like this:

- (id)viewController
{
  return [TyphoonDefinition withClass:[MyViewController class] configuration:^(TyphoonDefinition *definition) {
    [definition injectProperty:@selector(view) with:[self myView]];
  }];
}

- (id)myView
{
  return [TyphoonDefinition withClass:[MyView class] configuration:^(TyphoonDefinition *definition) {
    [definition injectProperty:@selector(delegate) with:[self viewController]];
  }];
}

From what I can see the view controller can never be used a reference as it doesn't added a shared reference anywhere and Typhoon has no option but to instantiate a new one?

@alexgarbarev
Copy link
Contributor

Yeah. That is problem, since viewController created by Storyboard and not owned by typhoon (which required to work with circular references)
I think there is not way to wire delegate to viewController with Typhoon. But you can try wire by InterfaceBuilder: mark your delegate property as IBOutlet and connect to controller.

@jasperblues
Copy link
Member

We like to have use GH for bug tracking and general support on Stack Overflow. The advantage of StackOverflow is wider community involvement and therefore faster response time, and also its very well indexed by Google for future users. So, I hope you don't but I've copied your question here:

http://stackoverflow.com/questions/27986510/typhoon-wire-circular-dependency-in-storyboard-created-view-controller/27986663#27986663

@jasperblues
Copy link
Member

If we do end up discovering a bug a feature request we can always reopen a new issue. Thanks for choosing Typhoon.

@foulkesjohn
Copy link
Author

My initial thoughts are that it should be straight forward to keep references to any storyboard created through TyphoonStoryboard and therefore easy to hook up them up as dependencies?

@jasperblues jasperblues reopened this Jan 16, 2015
@jasperblues
Copy link
Member

There's a TyphoonStack used for resolving circular dependencies along with an object-graph (default) scope pool. Perhaps we can push on this stick prior to instructing Typhoon to inject deps on the instance.

@alexgarbarev
Copy link
Contributor

@jasperblues Agree. We can leave this as feature request and do that special case in order of TyphoonStoryboard.. (i.e. making TyphoonStoryboard more integrated into Typhoon-core)

@jasperblues
Copy link
Member

@foulkesjohn I made a change to that I think should work, but can't do unit tests at the moment. Can you try update to:

pod 'Typhoon', :head

And let me know how it goes?

@foulkesjohn
Copy link
Author

Doesn't seem have made a difference, not sure where to look to try and debug it either. Where should I be looking to try track down whats wrong?

@jasperblues
Copy link
Member

Oops. I only took you part of the way there. Can you try again?

Where should I be looking to try track down whats wrong?

Circular dependencies can be tricky because there's lots of recursion involved. It relies on TyphoonCallStack, scopes and the id<TyphoonComponentPool>. See TyphoonComponentFactory+InstanceBuilder . . in the case of a pre-obtained instance, the stack and shared pools weren't being intitialized pre to hooking into the property injector, hence circular deps wouldn't work.

@jasperblues
Copy link
Member

One more change, and added the following test case, which passes:

- (void)test_first_resolves_circular_dependencies {
    StoryboardFirstlViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"first"];
    StoryboardControllerDependency *dependency = controller.dependency;
    StoryboardFirstlViewController *circular = dependency.circularDependencyBackToViewController;

    XCTAssertTrue(controller == circular);
}

Thanks for your patience.

@foulkesjohn
Copy link
Author

That seems to have worked, thanks for looking at it so quickly

@jasperblues
Copy link
Member

We'll push this out as a minor version then, so you don't have to pod from :head . . pushing now.

@jasperblues
Copy link
Member

Pushed Typhoon 2.3.4 to CocoaPods master repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants