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

If waiting for push device details and got them persisted, re-emit them. #967

Merged
merged 3 commits into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 13 additions & 21 deletions Source/ARTPush.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,17 @@ - (ARTPushActivationStateMachine *)activationMachine {
activationMachine_once_token = &once;
static id activationMachineInstance;
dispatch_once(&once, ^{
activationMachineInstance = [[ARTPushActivationStateMachine alloc] init:self->_rest];
// -[UIApplication delegate] is an UI API call, so needs to be called from main thread.
__block id delegate = nil;
if ([NSThread isMainThread]) {
delegate = UIApplication.sharedApplication.delegate;
} else {
dispatch_sync(dispatch_get_main_queue(), ^{
delegate = UIApplication.sharedApplication.delegate;
});
}

activationMachineInstance = [[ARTPushActivationStateMachine alloc] init:self->_rest delegate:delegate];
});
return activationMachineInstance;
}
Expand Down Expand Up @@ -169,29 +179,11 @@ + (void)didFailToRegisterForRemoteNotificationsWithError:(NSError *)error rest:(
}

- (void)activate {
if (!self.activationMachine.delegate) {
dispatch_async(dispatch_get_main_queue(), ^{
// -[UIApplication delegate] is an UI API call
self.activationMachine.delegate = UIApplication.sharedApplication.delegate;
[self.activationMachine sendEvent:[ARTPushActivationEventCalledActivate new]];
});
}
else {
[self.activationMachine sendEvent:[ARTPushActivationEventCalledActivate new]];
}
[self.activationMachine sendEvent:[ARTPushActivationEventCalledActivate new]];
}

- (void)deactivate {
if (!self.activationMachine.delegate) {
dispatch_async(dispatch_get_main_queue(), ^{
// -[UIApplication delegate] is an UI API call
self.activationMachine.delegate = UIApplication.sharedApplication.delegate;
[self.activationMachine sendEvent:[ARTPushActivationEventCalledDeactivate new]];
});
}
else {
[self.activationMachine sendEvent:[ARTPushActivationEventCalledDeactivate new]];
}
[self.activationMachine sendEvent:[ARTPushActivationEventCalledDeactivate new]];
}

#endif
Expand Down
1 change: 1 addition & 0 deletions Source/ARTPushActivationStateMachine+Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ extern NSString *const ARTPushActivationPendingEventsKey;

@property (nonatomic, strong) ARTRestInternal *rest;
- (instancetype)init:(ARTRestInternal *)rest;
- (instancetype)init:(ARTRestInternal *)rest delegate:(nullable id)delegate;

@property (weak, nonatomic) id delegate; // weak because delegates outlive their counterpart
@property (nonatomic, copy, nullable) void (^transitions)(ARTPushActivationEvent *event, ARTPushActivationState *from, ARTPushActivationState *to);
Expand Down
13 changes: 13 additions & 0 deletions Source/ARTPushActivationStateMachine.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ @implementation ARTPushActivationStateMachine {
}

- (instancetype)init:(ARTRestInternal *)rest {
return [self init:rest delegate:nil];
}

- (instancetype)init:(ARTRestInternal *)rest delegate:(id)delegate {
if (self = [super init]) {
_rest = rest;
_delegate = delegate;
_queue = _rest.queue;
_userQueue = _rest.userQueue;
// Unarchiving
Expand All @@ -52,6 +57,14 @@ - (instancetype)init:(ARTRestInternal *)rest {
if (!_pendingEvents) {
_pendingEvents = [NSMutableArray array];
}

// Due to bug #966, old versions of the library might have led us to an illegal
// persisted state: we have a deviceToken, but the persisted push state is WaitingForPushDeviceDetails.
// So we need to re-emit the GotPushDeviceDetails event that led us there.
if ([_current isKindOfClass:[ARTPushActivationStateWaitingForPushDeviceDetails class]] && rest.device_nosync.deviceToken != nil) {
[rest.logger debug:@"ARTPush: re-emitting stored device details for stuck state machine"];
[self handleEvent:[ARTPushActivationEventGotPushDeviceDetails new]];
QuintinWillison marked this conversation as resolved.
Show resolved Hide resolved
}
}
return self;
}
Expand Down
18 changes: 18 additions & 0 deletions Spec/PushActivationStateMachine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,25 @@ class PushActivationStateMachine : QuickSpec {
}

}

// https://github.com/ably/ably-cocoa/issues/966
it("when initializing from persistent state with a deviceToken, GotPushDeviceDetails should be re-emitted") {
storage = MockDeviceStorage(startWith: ARTPushActivationStateWaitingForPushDeviceDetails(machine: initialStateMachine))
rest.internal.storage = storage
rest.device.setAndPersistDeviceToken("foo")
defer { rest.device.setAndPersistDeviceToken(nil) }

var registered = false

let delegate = StateMachineDelegateCustomCallbacks()
stateMachine = ARTPushActivationStateMachine(rest.internal, delegate: delegate)
delegate.onPushCustomRegister = { error, deviceDetails in
registered = true
return nil
}

expect(registered).toEventually(beTrue())
}
}

// RSH3c
Expand Down