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

RCT_EXPORT_METHOD always keeps the same value for NSMutuableArray #23513

Closed
WojTherapy opened this issue Feb 18, 2019 · 8 comments
Closed

RCT_EXPORT_METHOD always keeps the same value for NSMutuableArray #23513

WojTherapy opened this issue Feb 18, 2019 · 8 comments
Labels
Bug Platform: iOS iOS applications. Resolution: Locked This issue was locked by the bot.

Comments

@WojTherapy
Copy link

WojTherapy commented Feb 18, 2019

🐛 Bug Report

Basically I need to pass and array of objects back to JavaScript every time when I call it from Javascript. My RCT bridge does certain things with array and updates it every time text on my Entry changes. My NSMutableArray is declared in interface so I can access it globally across whole class. Later on I make a call from Javascript to get a new Array, however Array value inside the RCT_EXPORT_METHOD always keeps the first value. It never gets updated.

Expected Behavior

NSMututableArray that I pass back to Javascript should get a new value that has been modified outside the method. Instead it keeps old data. Never updates.

Code Example

RCT_EXPORT_METHOD(findEvents: (NSString *) test
                 findEventsWithResolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  NSError *eErr;
  if(predictionData != nil){
    NSArray *events = predictionData;
    if (events) {
      resolve(events);
    }
    else {
      eErr = [[NSError alloc] initWithDomain:@"Predictions error"
                                        code:200 userInfo:nil];
      reject(@"no_events", @"There were no events", eErr);
    }
  }
}
- (void) dataReady:(NSArray*) data
{
//  self.predictionData = [[NSMutableArray alloc] init];
  bool twoWord = false;
  [predictionData removeAllObjects];
  if(twoWord){
    [predictionData addObjectsFromArray:data[0] ];
  }
  else{
     [predictionData addObjectsFromArray:data[1] ];
  }
}

Environment

React Native Environment Info:
System:
OS: macOS High Sierra 10.13.6
CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
Memory: 525.88 MB / 16.00 GB
Shell: 5.3 - /bin/zsh
Binaries:
Node: 10.15.0 - /usr/local/bin/node
npm: 6.4.1 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
Android SDK:
API Levels: 23, 24, 25, 26, 27, 28
Build Tools: 27.0.3, 28.0.3
System Images: android-27 | Google Play Intel x86 Atom
IDEs:
Android Studio: 3.3 AI-182.5107.16.33.5264788
Xcode: 10.1/10B61 - /usr/bin/xcodebuild
npmPackages:
react: ^16.6.3 => 16.8.2
react-native: ^0.58.4 => 0.58.4
npmGlobalPackages:
react-native-cli: 2.0.1
react-native-git-upgrade: 0.2.7

@WojTherapy
Copy link
Author

Please bear in mind that placing breakpoint inside "dataReady" method shows that value does get updated. Somehow predictionData stays the same inside the RCT_EXPORT_METHOD. I also tried using RCT_REMAP_METHOD, with the same outcome.

@react-native-bot
Copy link
Collaborator

It looks like you are using an older version of React Native. Please update to the latest release, v0.58 and verify if the issue still exists.

The "Resolution: Old Version" label will be removed automatically once you edit your original post with the results of running react-native info on a project using the latest release.

@WojTherapy
Copy link
Author

Also, since you forced me to update to newest react-native it broke my remote debugger for iOS (Android remote debugger is already broke), so i cannot debug my javascript anymore. Debugger issue: #23235

Thx in advance.

@hramos
Copy link
Contributor

hramos commented Feb 19, 2019

I am sorry you ran into that other issue. No one's forcing anyone to upgrade. We're interested in tracking issues that are known to be present in the latest released version, to keep maintainers from spending time chasing issues that might already be fixed.

@karanjthakkar
Copy link
Contributor

karanjthakkar commented Feb 21, 2019

Debugger issue: #23235

@WojTherapy The issue you mentioned has been fixed in the 0.59.0-rc.2 and has also been backported to 0.58.5. So you should be able to use the debugger without any issues now.

NSMututableArray that I pass back to Javascript should get a new value that has been modified outside the method. Instead it keeps old data. Never updates.

Regarding the original issue, could you please create a repo with the relevant native code that shows the error so we can take a look? Nonetheless, I don't think your issue has anything to do with React Native in itself because it doesn't do any special handling for functions and variables inside it. It could be a bug in the Objective C code that you have.

@WojTherapy
Copy link
Author

WojTherapy commented Feb 21, 2019

I dont think it is Objective C bug as changing the value of my variable in any other method, at any other point behaves as expected. The only place where it retains just the first value is within the RCT_REMAP_METHOD or RCT_EXPORT_METHOD. Passing props back to JavaScript behaves as expected as I receive them in JS, its just that the value is wrong(due to retaining original value). When you put breakpoint inside those react methods, the value is always the same, always, unless I modify it within the RCT_REMAP_METHOD/RCT_EXPORT_METHOD. It seems like Macro never gets updated unless value is modified within those methods. I will create a repo on the weekend when I get a minute. It is worth noting that I solved the issue by making the variable static "static NSArray *predictionData;", but Id rather not to use static fields.

Thx for answer, and sorry for bit of attitude. I have been very frustrated with React Native recently.

@karanjthakkar
Copy link
Contributor

@WojTherapy I'm happy to help you figure out what's happening once you provide a repro. For now what you can do is, compare the self indataReady(which you say is called first) and findEvents(which is called after). Ideally they should be the same. But if you say that using static solves your problem, it means that they are not. In which case something is causing the bridge to reload which can cause the instance to be reset and lose your old value. Again, a minimal reproduction would help figure out the root cause.

@dulmandakh dulmandakh added the Platform: iOS iOS applications. label Mar 17, 2019
@rickhanlonii
Copy link
Member

If we can get a repo for this we're happy to re-open and take a look 👍

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

No branches or pull requests

6 participants