Skip to content
New issue

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

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

Already on GitHub? # to your account

State Restoration #65

Closed
djibouti33 opened this issue Jul 29, 2013 · 5 comments
Closed

State Restoration #65

djibouti33 opened this issue Jul 29, 2013 · 5 comments
Labels

Comments

@djibouti33
Copy link
Contributor

Hey there,

First off, thanks so much for the work you've done with this library. So simple to use and such a nice lib.

I'm trying to add State Restoration to my app and am running into a problem. I'm not sure if it's in the code (the state restoration machinery) that I've set up or if something unexpected is happening in your lib that I'm not aware of (I haven't been able to find anything yet).

I have an SO question open: m/questions/17934668/ios-state-restoration-with-custom-container-view-controller-mmdrawercontroller but I'll also post here since it will be easier to talk about.

I've added this to MMDrawerController:

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    [coder encodeObject:self.centerViewController forKey:@"centerVC"];
    [coder encodeObject:self.leftDrawerViewController forKey:@"leftDrawerVC"];
    [super encodeRestorableStateWithCoder:coder];
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    // these values log out fine if I don't interact with the left Drawer.
    // if i do, they wind up being null
    NSLog(@"leftDrawer: %@", [coder decodeObjectForKey:@"leftDrawerVC"]);
    NSLog(@"center: %@", [coder decodeObjectForKey:@"centerVC"]);
    [super decodeRestorableStateWithCoder:coder];
}

Essentially I'm storing the left and center view controllers so that when restoration happens, I can get them back and add them to the main MMDrawerController.

In the first CenterViewController that loads, I added two buttons that change the color of the background. I can change it to red, for example, kill the app, and come back in and the background is still red. Perfect.

However, if I open the left drawer, and select an item which swaps out the center view controller, when I quit the app and come back in, during restoration the two logs in decodeRestorableStateWithCoder are now null.

For some reason changing out the center view controller messes with the coder.

Again, I'm not sure if the problem is in my code or if something unexpected is happening, but I've been on this for a few days and figure I should ask.

Thanks!

@kcharwood
Copy link
Contributor

I must confess I haven't done much with state restoration in the past, so I'm a little unclear as to what is causing the problem. State restoration should certainly be a first class citizen of this library, so I'm interested in learning what the issue is and how we should be handling it. Keep me updated on your progress. I'll try and find some time to dig in as well.

@djibouti33
Copy link
Contributor Author

That's totally fair.

I'm currently working with an Apple Engineer to help debug the problem, and I'll absolutely let you know what I find out. Hopefully it will be in the form of a Pull Request.:)

@jsankey
Copy link

jsankey commented Aug 1, 2013

I've answered this question on SO, I think the problem is outside of MMDrawerController. However as noted in the report here it would be useful if MMDrawerController implemented en/decoreRestorableStateWithCoder. I've had to do very similar to above in an MMDrawerController subclass to get restoration working in my app (see below). I'm not sure if there is any drawback to including this in the library, but it seems to me most apps will want to implement restoration, and having a drawer at the root makes something like the below necessary.

static NSString *STATE_ID_LEFT = @"leftDrawer";
static NSString *STATE_ID_RIGHT = @"rightDrawer";
static NSString *STATE_ID_CENTER = @"centerDrawer";

@implementation POZDrawerController

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
    [super encodeRestorableStateWithCoder:coder];
    if (self.leftDrawerViewController)
    {
        [coder encodeObject:self.leftDrawerViewController forKey:STATE_ID_LEFT];
    }

    if (self.rightDrawerViewController)
    {
        [coder encodeObject:self.rightDrawerViewController forKey:STATE_ID_RIGHT];
    }

    if (self.centerViewController)
    {
        [coder encodeObject:self.centerViewController forKey:STATE_ID_CENTER];
    }
}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
    [super decodeRestorableStateWithCoder:coder];
    UIViewController *left = [coder decodeObjectForKey:STATE_ID_LEFT];
    if (left)
    {
        self.leftDrawerViewController = left;
    }

    UIViewController *right = [coder decodeObjectForKey:STATE_ID_RIGHT];
    if (right)
    {
        self.rightDrawerViewController = right;
    }

    UIViewController *center = [coder decodeObjectForKey:STATE_ID_CENTER];
    if (center)
    {
        self.centerViewController = center;
    }
}

@end

@djibouti33
Copy link
Contributor Author

Exactly. I'm going to be opening up a Pull Request soon with changes very similar to this, once I get it working. I just looked at your answer on SO and I think I know the problem. Thanks so much for responding and I'll update here when I've got something working.

@djibouti33
Copy link
Contributor Author

Closing this since it's not MMDrawerController's problem

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants