简体   繁体   English

iOS应用程序的后台更新问题

[英]Background update issue on iOS app

I'm trying to make an "auto-update" process to make the user stay updated every X minutes while the app's awake. 我正在尝试进行“自动更新”过程,以使用户在应用唤醒时每隔X分钟保持更新。 What I'm doing it's a background call every X minutes that asks the server if there are updates to make or not. 我正在做的是每隔X分钟拨打一次后台电话,询问服务器是否有要进行的更新。

The thing is that when I make a call and the server returns YES, I've got to to update two things: my local DB and also the user interface (cause there might be changes on the interface too). 事实是,当我拨打电话并且服务器返回YES时,我必须更新两件事:我的本地数据库以及用户界面(因为界面上可能也会发生变化)。

What I've got now is a thread problem. 我现在得到的是一个线程问题。 If I don't make any drawing while the app's updating I've got no issue, but when I do the app crashes. 如果我在应用程序更新时不作任何绘图,则没有问题,但是当我执行该应用程序时会崩溃。

Any ideas how could I control that threading issue? 有什么想法可以控制该线程问题吗?

I'm assuming you are using CoreData for your database. 我假设您正在为数据库使用CoreData。 You need to make sure that any updates you make to the managed object context occur on the thread on which that context was created (typically the main thread). 您需要确保对托管对象上下文所做的任何更新都在创建该上下文的线程(通常是主线程)上进行。

You also need to make sure that any updates you make to the user interface also occur on the main thread. 您还需要确保对用户界面所做的任何更新也都在线程上进行。

Maybe try running the code on a background thread. 也许尝试在后台线程上运行代码。 one easy way to do this is preformSelectorOnBackgroundThread method... 一种简单的方法是preformSelectorOnBackgroundThread方法...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM