3

我有个问题。我仍然是 react-native 的新手,但我找不到与我的问题相关的任何内容。谷歌也没有在 react/react-native 文档中。

我在上一个项目中已经遇到了这个问题,但我从未完成它,也没有尝试解决它。

所以问题是:

如果我使用this.setState()or this.props.navigator.resetTo()from react-native-navigation,它只会在我触摸显示器上的某个位置或旋转我的设备之前什么都不做。此外,聚焦和取消聚焦 ios 模拟器就足够了。

似乎 JS 线程被暂停,直到我输入一些东西。当我使用mobx observable. 但是对于 a observable,它发生的频率并不比 with 多setState()。这就是为什么我将所有本地状态转移到本地 mobx 商店,它改善了我的情况,但有时仍会发生 react-native 以某种方式等待触摸输入。我还尝试过的一件事是将每个都包装resetTo()在一个 中requestAnimationFrame(),这也稍微改善了我的情况。

现在一个示例代码:

这是我打开领域数据库的初始屏幕。

import React, {Component} from 'react';
import {View, Dimensions} from 'react-native';
import Schema from '../Schema';
import {Everything, Untis, NavigateEverywhere} from '../Stores/index';
import {Text} from 'react-native-elements';
// import * as Progress from 'react-native-progress';
import percentage from 'percentage-calc';
import Realm from 'realm';
import {observable} from "mobx"
import {observer} from 'mobx-react';
import {MKProgress} from 'react-native-material-kit';

@observer
class Database extends Component {

	constructor(props) {
		super(props);
		console.ignoreWarnings = ['Realm.openAsync'];
		NavigateEverywhere.setNavigator(this.props.navigator);
	}


	// state = {
	// 	statusText: 'Initializing Database...',
	// 	uploadProgress: 0,
	// 	downloadProgress: 0,
	// 	uploadMax: 0,
	// 	downloadMax: 0,
	// 	uploadCurrent: 0,
	// 	downloadCurrent: 0,
	// 	workingUpload: false,
	// 	workingDownload: false,
	// 	realmHost: this.realmHost,
	// 	realmServer: `http://${this.realmHost}/`,
	// 	realm: `realm://${this.realmHost}/~/sitnu`
	// };

	@observable statusText = 'Initializing Database...';
	@observable uploadProgress = 0;
	@observable downloadProgress = 0;
	@observable uploadMax = 0;
	@observable downloadMax = 0;
	@observable uploadCurrent = 0;
	@observable downloadCurrent = 0;
	@observable workingUpload = false;
	@observable workingDownload = false;
	realmHost = '';
	realmServer = `http://${this.realmHost}/`;
	realm = `realm://${this.realmHost}/~/sitnu`;

	componentDidMount() {
		this.bootStrap().catch(console.error);
	}

	async bootStrap() {
		let user;
		if (this.props.token && this.props.provider) {
			try {
				user = await Realm.Sync.User.registerWithProvider(this.realmServer, {
					provider: this.props.provider,
					providerToken: this.props.token
				});
			} catch (e) {
				return this.props.navigator.resetTo({
					screen: 'io.LoginRealm',
					title: 'Login into Realm',
					passProps: {
						error: e
					}
				});
			}
		}
		if (this.props.username && this.props.password) {
			try {
				user = await new Promise((resolve, reject) => {
					Realm.Sync.User.login(this.realmServer, this.props.username, this.props.password, (err, u) => {
						if (err) return reject(err);
						resolve(u);
					});
				});
			} catch (e) {
				return this.props.navigator.resetTo({
					screen: 'io.LoginRealm',
					title: 'Login into Realm',
					passProps: {
						error: e
					}
				});
			}
		}

		let users = [];
		for (let key in Realm.Sync.User.all) {
			users.push(Realm.Sync.User.all[key]);
		}
		if (users.length !== 0 && !user) {
			user = users[0];
		}

		if (!user) {
			return this.props.navigator.resetTo({
				screen: 'io.LoginRealm',
				title: 'Login into Realm',
				passProps: {
					error: 'user is undefined or null'
				}
			});
		}
		let realm;
		try {
			realm = await new Promise((resolve, reject) => {
				Realm.openAsync({
					schema: Schema,
					sync: {
						user: user,
						url: this.realm
					}
				}, (error, realm) => {
					if (error) return reject(error);
					resolve(realm);
				}, this.downloadCallback.bind(this));
			});
		} catch (e) {
			console.log("Why");
			return requestAnimationFrame(() => {
				this.props.navigator.resetTo({
					screen: 'io.LoginRealm',
					title: 'Login into Realm',
					passProps: {
						error: e
					}
				});
			});
		}

		this.workingUpload = false;
		this.workingDownload = false;
		this.statusText = 'Finishing Database...';

		Everything.setRealm(realm);

		const username = realm.objectForPrimaryKey('KeyValue', 'username');
		const password = realm.objectForPrimaryKey('KeyValue', 'password');
		const host = realm.objectForPrimaryKey('KeyValue', 'host');
		const school = realm.objectForPrimaryKey('KeyValue', 'school');
		const setup = realm.objectForPrimaryKey('KeyValue', 'setup');
		if (typeof username === 'object' && typeof password === 'object' && typeof host === 'object' && typeof school === 'object' && typeof setup === 'object' && username.value && password.value && host.value && school.value && setup.value) {
			Everything.setCredentials(username.value, password.value, host.value, school.value);
			Untis.setSettings(username.value, password.value, host.value, school.value);
			requestAnimationFrame(() => {
				this.props.navigator.resetTo({
					screen: 'io.Home',
					animated: true,
					title: 'Home'
				});
			});
		} else {
			requestAnimationFrame(() => {
				this.props.navigator.resetTo({
					screen: 'io.Login',
					animated: true,
					navigatorStyle: {
						navBarHidden: true,
						statusBarTextColorSchemeSingleScreen: 'dark'
					}
				});
			});
		}
	}

	downloadCallback = async (transferred, transferable) => {
		this.workingDownload = true;
		this.downloadMax = transferable;
		this.downloadCurrent = transferred;
		this.downloadProgress = percentage.from(transferred, transferable) / 100;
		this.statusText = 'Sync Database';
	};

	render() {
		return (
			<View style={{
				alignContent: "center",
				alignItems: "center",
				alignSelf: "center",
				flex: 1,
				justifyContent: "center"
			}}>
				<Text h5>{this.statusText ? <Text>{this.statusText}</Text> : null}</Text>
				{this.workingUpload ? <View>
					{/*<Progress.Bar progress={this.uploadProgress}/>*/}
					<MKProgress style={{width: Dimensions.get('window').width - 40}} progress={this.uploadProgress}/>
					<Text>Upload {this.uploadCurrent ?
						<Text>{this.uploadCurrent}</Text> : null}/{this.uploadMax ?
						<Text>{this.uploadMax}</Text> : null}</Text>
				</View> : null}
				{this.workingDownload ? <View style={{
					alignContent: 'center',
					alignItems: 'center',
					alignSelf: 'center',
					justifyContent: 'center'
				}}>
					{/*<Progress.Bar progress={this.downloadProgress}/>*/}
					<MKProgress style={{width: Dimensions.get('window').width - 40}} progress={this.downloadProgress}/>
					<Text>Download {this.downloadCurrent ?
						<Text>{this.downloadCurrent}</Text> : null}/{this.downloadMax ?
						<Text>{this.downloadMax}</Text> : null}</Text>
				</View> : null}
			</View>
		);
	}

}

export default Database;

也许这只是一些完全愚蠢的事情,我只是没有看到,但我已经尝试了很多事情,不知道该怎么做。

问候尼尔斯

4

2 回答 2

0

我曾经遇到过类似的问题。这个问题来自 react-native 调试桥。尝试禁用调试模式。或者,如果它不起作用,请尝试生成并安装发布版本。

于 2017-10-01T13:19:54.107 回答
0

右键单击浏览器并打开检查元素模式。然后选择调试器菜单。在那里你会找到一个选项“错误时暂停脚本执行”。启用此选项。如果任何线程停止或出现任何错误,脚本执行将停止,您可以在控制台上看到错误。如果您仍然遇到问题,请告诉我。

于 2017-10-03T10:29:26.210 回答