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

[Timer] EventDispatcher sendDeviceEventWithName only called once. #1218

Closed
mintuz opened this issue May 9, 2015 · 12 comments
Closed

[Timer] EventDispatcher sendDeviceEventWithName only called once. #1218

mintuz opened this issue May 9, 2015 · 12 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@mintuz
Copy link

mintuz commented May 9, 2015

Hello, I think the EventDispatcher sendDeviceEventWithName has a bug in which the EventDispatcher sendDeviceEventWithName is only called once initially until you close the app down to the background and then reopen it. Once you open it the Events seem to be triggered as expected.

I have created a native bridge to a library located https://github.com/omergul123/Discovery It's also worth pointing out that the library works fine whilst not in a React Native project.

My bridge looks like this

//
//  DiscoveryManager.m
//  DiscoveryExample
//
//  Created by Adam Bulmer on 07/05/2015.
//  Copyright (c) 2015 Adam Bulmer All rights reserved.
//

#import <Foundation/Foundation.h>
#import "DiscoveryManager.h"

@implementation DiscoveryManager

  @synthesize bridge = _bridge;

  RCT_EXPORT_MODULE()

  RCT_EXPORT_METHOD(startAdvertising:(NSString *)uuidString
                    username:(NSString*)username) {

    NSMutableArray *usernameArrays = [[NSMutableArray alloc] init];
    NSLog(@"startAdvertising called");

    // create our App UUID.
    CBUUID *uuid = [CBUUID UUIDWithString:uuidString];

    // start Discovery API
    self.discovery = [[Discovery alloc] initWithAppUUID:uuid username:username usersBlock:^(NSArray *users, BOOL usersChanged) {

      NSLog(@"users found in ObjC - %@", users);

      // Hacky
      for (BLEUser *user in users) {
        [usernameArrays addObject:user.username];
      }

      [self.bridge.eventDispatcher sendDeviceEventWithName:@"FoundUsers" body:@{@"users":usernameArrays}];
    }];
  }

@end

The Javascript component looks like this.

var React = require('react-native');
var LoginView = require('./App/views/Login.ios.js');
var AuthStore = require('./App/stores/AuthStore.ios.js');
var DiscoveryManager = require('NativeModules').DiscoveryManager;

const APP_UUID = '6969EFE7-C07E-4497-95E7-105C01152421';

var {
  View, 
  DeviceEventEmitter, 
  AlertIOS
} = React;

var App = React.createClass({
  getInitialState: function() {

    return {
      loggedIn: false
    };
  },
  onAuthChange: function(authObj) {

    this.setState({loggedIn:authObj});
    DiscoveryManager.startAdvertising(APP_UUID, 'mintuz');
  },
  onUsersChange: function(obj) {

    console.log(obj);

    AlertIOS.alert(
      'User Found',
      obj.users[0]
    );
  },
  componentWillMount: function() {

    console.log("will mount");

    AuthStore.listen(this.onAuthChange);
    DeviceEventEmitter.addListener('FoundUsers', this.onUsersChange);
  },
  render: function() {

    if(!this.state.loggedIn){
      return (<LoginView></LoginView>);
    }

    return (<View style={{flexDirection: 'row', height: 100, padding: 20}}>
      <View style={{backgroundColor: 'blue', flex: 0.3}} />
      <View style={{backgroundColor: 'red', flex: 0.5}} />
    </View>);
  }
});

React.AppRegistry.registerComponent('TestAppSpike', () => App);
@mintuz mintuz changed the title EventDispatcher sendDeviceEventWithName only called once. bug - EventDispatcher sendDeviceEventWithName only called once. May 10, 2015
@mintuz
Copy link
Author

mintuz commented May 10, 2015

To add to this I have found out that the block for Discovery library isn't called multiple times until you close the app and reopen it. It does however work if its not called within a React Native environment. Looking at the Discovery library the only things that I think could be breaking with React Native are the timers and queues that are being set. Any help would be appreciated.

@ide
Copy link
Contributor

ide commented May 10, 2015

RN pauses timers when the app is backgrounded. Not sure why this is -- it probably shouldn't do this at the infrastructure level.

@mintuz
Copy link
Author

mintuz commented May 10, 2015

The timers don't seem to fire until the app is closed and reopened.

@vjeux
Copy link
Contributor

vjeux commented May 14, 2015

cc @nicklockwood and @a2, this is weird that timers are suppressed

@a2
Copy link
Contributor

a2 commented May 14, 2015

I assume this has to do with the way run loops (and run loop modes) work?

@mintuz
Copy link
Author

mintuz commented May 14, 2015

I know NSTimers work without the use of React Native within a standard native app.

@ide
Copy link
Contributor

ide commented May 14, 2015

@a2 that could be part of it since the run loop is driven by the display link. This for sure is part of the problem:

[[NSNotificationCenter defaultCenter] addObserver:self

    for (NSString *name in @[UIApplicationWillResignActiveNotification,
                             UIApplicationDidEnterBackgroundNotification,
                             UIApplicationWillTerminateNotification]) {

      [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(stopTimers)
                                                   name:name
                                                 object:nil];
    }

@a2
Copy link
Contributor

a2 commented May 14, 2015

@ide That would definitely do it! 😕

@brentvatne brentvatne changed the title bug - EventDispatcher sendDeviceEventWithName only called once. [Timer] EventDispatcher sendDeviceEventWithName only called once. May 29, 2015
@mintuz
Copy link
Author

mintuz commented Jul 11, 2015

Any update on this?

@ghost
Copy link

ghost commented Aug 5, 2015

Thank you for reporting this issue and appreciate your patience. We've notified the core team for an update on this issue. We're looking for a response within the next 30 days or the issue may be closed.

@mintuz
Copy link
Author

mintuz commented Aug 18, 2015

Any update on this, sorry for the spam. Just seems to have gone quiet.

@brentvatne
Copy link
Collaborator

Hi there! This issue is being closed because it has been inactive for a while.

But don't worry, it will live on with ProductPains! Check out it's new home: https://productpains.com/post/react-native/timer-eventdispatcher-senddeviceeventwithname-only-called-once

@facebook facebook locked as resolved and limited conversation to collaborators Jul 22, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

6 participants