CM3050 Topic 06: Data Sources
Main Info
Title: Data Sources
Teachers: Joe McAlister
Semester Taken: April 2022
Parent Module: cm3050: Mobile Development
Description
In this topic, we discuss what are data sources? Including the important ethical issues associated with data. Later taking a focus on local data.
Key Reading
Lecture Summaries
6.0 Data Sources and Ethics
Runs through the process of fetching data via web APIs.
Going to focus first on local data, then look at remote data.
Introduces ethics of data collection.
First think about requirements - do you need to collect data? If not, don’t.
Introduces the perils of accidentally revealing info even when producing aggregate data. Check the laws of your country.
Physical security - prioritize on-device storage, if necessary use encrypted remote storage. You can use on device encryption.
6.1 JSON and XML
You’ll often encounter JSON and XML whenever you’re handling data.
We store values in a text form for transmission and storage.
Introduces JSON and XML syntax. Javascript has built in JSON parsing and serialization through the JSON
object.
for XML, we need an xml parser library, we can get one with npm i fast-xml-parser
Then:
import {parse} from 'fast-xml-parser'
let parsedString = parse(xmlString);
let nameNode = parsedString.root.name;
Shows importing a JSON file.
Using ES6 syntax in react native you can just import the file as you would a module, and the resulting variable will be the parsed JSON object: import JSONdata from './myFile.json'
6.2 Saving Data
You’ll often need to save data to the phone. There are two libraries for this.
The AsyncStorage
library is recommended by expo as the current react natvie component is deprecated.
Install with expo install @react-native-async-storage/async-storage
Stores data asynchronously in unencrypted form.
Can only store strings. Stringify the JSON object.
SecureStore stores data in an encrypted form, using the keychain services in iOS and the keystore system in Android. Install using expo install expo-secure-store
On iOS can persist data across installs. Use for login tokens, anything critical. Size limit is 2048 bytes.
Here’s a simple example of using Async storage:
import AsyncStorage from '@react-native-async-storage/async-storage';
export default function App() {
const [text, onChangeText] = useState("");
// should really have a boot state...
if (text === "") {
AsyncStorage.getItem('@textInput').then((text) => {
onChangeText(text);
}).catch( (err) => console.log(err));
}
return (
<TextInput onChangeText={async (text) => {
await AsyncStorage.setItem('@textInput', text);
onChangeText(text);
}} />
)
6.3 Final Project
Final project is a larger more substantial project, genre is up to you. Should take 35 hours!
Guidance on how to approach it/how it will be marked is provided over the remaining weeks.