Parse.com查询的主线程问题 [英] Main thread issue with Parse.com queries

查看:73
本文介绍了Parse.com查询的主线程问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将parse.com服务与我的统一游戏一起使用.我的问题是根据从查询中收到的结果实例化对象.

I am trying to use parse.com service with my unity game. My problem is instantiating objects according to the results received from a query.

例如,当我运行以下代码时;

For example, when I run the below code;

var queryCurrent = ParseObject.GetQuery("Levels")
.WhereEqualTo("ItemId", "Character")
.WhereEqualTo("Level", "3");

queryCurrent.FirstAsync().ContinueWith(t =>
{
    Character character = ScriptableObject.CreateInstance<Character>();
});

我收到以下错误;

CreateInstanceFromType只能从主线程调用. 构造函数和字段初始化程序将从加载中执行 加载场景时使用线程.不要在 构造函数或字段初始化程序,而是将初始化代码移至 唤醒或启动功能.

CreateInstanceFromType can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

这似乎是一个普遍的问题,每个人都试图通过协程找到一种解决方法.一个优化的解决方案将不胜感激.

It seems that this is a general problem and everyone tries to find a workaround by using coroutines. An optimized solution will be appreciated.

谢谢.

推荐答案

使用协程在主线程上运行代码.它在Parse网站上记录在这里: https://parse.com/docs/unity_guide#tasks-coroutines

Use a Coroutine to run things on the main thread. It's documented on the Parse site here: https://parse.com/docs/unity_guide#tasks-coroutines

Unity提供了协同多任务处理的协程概念,允许代码在所有线程都在主线程上运行时被交错.任务和延续模型大多独立于这种多任务处理机制,但很容易适应协同程序中的工作.您的协程只需检查任务的IsCompleted属性即可知道异步工作是否已完成,从而使协程可以从中断处继续执行.例如,以下协程代码保存一个对象,然后等待查询返回:

Unity provides the notion of a Coroutine for cooperative multitasking, allowing code to be interleaved while all running on the main thread. Tasks and the continuation model are mostly independent of this multitasking mechanism, but are easy to adapt to work within a coroutine. Your coroutine can simply check the IsCompleted property of a task to know whether the asynchronous work has completed, allowing the coroutine to continue where it left off. For example, the following coroutine code saves an object and then waits for a query to return:

public IEnumerator GameOver()
{
    var gameHistory = new ParseObject("GameHistory");
    gameHistory["score"] = score;
    gameHistory["player"] = ParseUser.CurrentUser;

    var saveTask = gameHistory.SaveAsync();
    while (!saveTask.IsCompleted) yield return null;

    // When the coroutine reaches this point, the save will be complete

    var historyQuery = new ParseQuery<ParseObject>("GameHistory")
        .WhereEqualTo("player", ParseUser.CurrentUser)
        .OrderByDescending("createdAt");

    var queryTask = historyQuery.FindAsync();
    while (!queryTask.IsCompleted) yield return null;

    // The task is complete, so we can simply check for its result to get
    // the current player's game history
    var history = queryTask.Result;
}

这篇关于Parse.com查询的主线程问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆