0

I am trying to solve an issue for my app, which is programmed in ReactNative.

I have a type of object named 'Card' which has two main variables, both of them strings: 'question' and 'answer'.

The data for this objects is provided to the app, which has to load them and show them to the user. I have around 10 thousand "points" of data written as lines in a .txt, each line being: "this is a question? // here is the answer".

I would like to create a Python script that breaks that .txt in 10 thousand files that can be read by a ReactNative method at run-time. I thought about AsyncStorage.getItem and wanted to ask how to format these files so that they can be read by the method.

But I'm beggining to think I would need to use Expo.FileSystem.readAsStringAsync(). Is that right? I would rather use AsyncStorage.getItem and just parse the file right away...

1 Answers1

0

AsyncStorage is not the thing you're looking for. It's a key-value-storage, which doesn't solve your problem as you're trying to use an already existing text file as database so to say. Expo's built in solution Expo.FileSystem.readAsStringAsync() seems to do exactly what you want. There are probably more ways to solve your problem, but using Expo's API is a pretty good solution it'd say.

If you want to improve performance I'd suggest using an actual database since you won't be needing to parse the txt file to the appropriate format. You could use an SQLite Database and just populate the data on the app's first launch similar to this approach How can I embed an SQLite database into an application?. Or you could call a remote api, but that's probably out of your scope.

marhaupe
  • 267
  • 1
  • 3
  • 10
  • Marcel, hello! Thank you very, very much for your answer. I'm shipping those "10,000" text files with the app inside a folder and trying to read them with the readAsStringAsync(). No success yet (documentation is poor and it's not my expertise), but will keep trying. I will too consider the embedded database! It is a great idea that I did not know was possible -- probably you improve performance by a lot. :) – Vittorio D'Angelo Dec 09 '18 at 12:18
  • I mean `AsyncStorage` is basically an SQLite Database aswell but I can't figure out how you would initially populate it. – marhaupe Dec 09 '18 at 13:53
  • I'm going crazy over this. I just want to ship some .txt files with the app and read them... What would take <25 lines of python in the desktop is seeming almost impossible (for me and my limitations) under ReactNative. – Vittorio D'Angelo Dec 11 '18 at 17:53
  • What have you tried so far? Feel free to create a new question and tag me once you did – marhaupe Dec 11 '18 at 19:29