Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 当应用程序每30分钟处于挂起/后台模式时,如何读取/更新本地sqlite数据?_Iphone_Sqlite_Ios5_Background Process - Fatal编程技术网

Iphone 当应用程序每30分钟处于挂起/后台模式时,如何读取/更新本地sqlite数据?

Iphone 当应用程序每30分钟处于挂起/后台模式时,如何读取/更新本地sqlite数据?,iphone,sqlite,ios5,background-process,Iphone,Sqlite,Ios5,Background Process,当应用程序在每秒钟后处于挂起/后台模式时,如何读取/更新本地sqlite数据? 下面是我试图做的代码 现在,我只想在每秒钟之后记录本地Sqlite表中存在的所有Id 但我们无法做到这一点。 有人能告诉我我做错了什么,或者有人能纠正下面的代码吗。 提前感谢 -(void)applicationDidEnterBackground:(UIApplication *)app { if ([[UIDevice currentDevice] respondsToSelector:@selector

当应用程序在每秒钟后处于挂起/后台模式时,如何读取/更新本地sqlite数据? 下面是我试图做的代码 现在,我只想在每秒钟之后记录本地Sqlite表中存在的所有Id 但我们无法做到这一点。 有人能告诉我我做错了什么,或者有人能纠正下面的代码吗。 提前感谢

-(void)applicationDidEnterBackground:(UIApplication *)app
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
        if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
            UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance

            __block UIBackgroundTaskIdentifier background_task; //Create a task object

            background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
                [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
                background_task = UIBackgroundTaskInvalid; //Set the task to be invalid

                //System will be shutting down the app at any point in time now
            }];

            //Background tasks require you to use asyncrous tasks

            dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            dispatch_source_t timerSource = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, backgroundQueue);
            dispatch_source_set_timer(timerSource, dispatch_time(DISPATCH_TIME_NOW, 0), 1*NSEC_PER_SEC, 0*NSEC_PER_SEC);
            dispatch_source_set_event_handler(timerSource, ^{
                [self BackGroundTaskFunction];
            });
            dispatch_resume(timerSource);
        }
    }
}
-(void)BackGroundTaskFunction
{
   [self GetUserName]
    if (recordArray.count>0) 
    {
        for (int i=0; i<recordArray.count; i++) 
        {
            DB_data *db=(DB_data *)[recordArray objectAtIndex:i];
            Nslog(@"%d",db.Id);          
        }
    }
}
-(void)GetUserName{
    self.recordArray = [[NSMutableArray alloc] init];
    {
        const char *sql = "select * from UserName";
        sqlite3_stmt *selectStatement;

        int returnValue = sqlite3_prepare_v2(database, sql, -1, &selectStatement, NULL);
        if(returnValue == SQLITE_OK)
        {
            while(sqlite3_step(selectStatement) == SQLITE_ROW)
            {
                [recordArray addObject:[[DB_data alloc] initwithDataSet:sqlite3_column_int(selectStatement, 0) UserName:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectStatement, 1)]]];
            }
        }
        else
        {
            printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
        }

    }

}
-(无效)应用程序标识符背景:(UIApplication*)应用程序
{
如果([[UIDevice currentDevice]respondsToSelector:@selector(isMultitaskingSupported)]{//请检查我们的iOS版本是否支持多任务处理,即iOS 4
如果([[UIDevice currentDevice]支持多任务]){//检查设备是否支持多任务
UIApplication*application=[UIApplication sharedApplication];//获取共享应用程序实例
__block UIBackgroundTaskIdentifier background_task;//创建任务对象
后台任务=[应用程序beginBackgroundTaskWithExpirationHandler:^{
[application endBackgroundTask:background_task];//告诉系统我们已经完成了任务
后台任务=UIBackgroundTaskInvalid;//将任务设置为无效
//系统将随时关闭应用程序
}];
//后台任务要求您使用异步任务
调度队列后台队列=调度获取全局队列(调度队列优先级默认为0);
dispatch\u source\u t timerSource=dispatch\u source\u create(dispatch\u source\u TYPE\u TIMER,0,0,backgroundQueue);
调度源设置定时器(timerSource,调度时间(dispatch\u time\u NOW,0),每秒1*NSEC\u,每秒0*NSEC\u);
调度\u源\u集\u事件\u处理程序(timerSource^{
[自我背景任务功能];
});
调度和恢复(timerSource);
}
}
}
-(void)BackGroundTaskFunction
{
[自我获取用户名]
如果(recordArray.count>0)
{

对于(int i=0;i否),您不能这样做,只有播放音频、跟踪用户位置或voip客户端的应用程序才允许在后台运行

如果您的应用程序不属于这些类型的应用程序之一,则您不能在后台运行


您发布的代码允许您运行一个最长10分钟的长过程。

我使用nsrunlop解决了我的问题,它帮助我使应用程序在后台保持运行

好的,那么请告诉我们,在后台模式下从本地sqlite在Web服务器上上载数据的应用程序有多少?他们没有,他们将
beginBackgroundTaskWithExpirationHan称为
beginBackgroundTaskWithExpirationHan德勒:
执行上载,但如果任务在10分钟内没有完成,他们将不得不等到用户再次启动应用程序后重试。10分钟应该足以将数据更新到web服务。如果你想从web服务提取数据,请在应用程序可用时这样做。我不明白为什么你必须更新data每30分钟一次。因为只有当应用程序激活或用户更改数据时才需要数据。只有在不断更新的情况下,您才会烧掉用户的电池和数据计划。好的,基本上我的工作是检查是否有用户更改Web服务器上的数据,它会自动下载到本地sqlite。这是我的客户要求,然后您需要创建ld让服务器检查这一点,并使用推送通知提醒用户数据已更改。SDK无法满足您的要求。但他说,它应该自动下载,无需任何警告等