后台程序刷新

时间:2014-05-30 13:16:29

标签: ios objective-c

在我的应用程序中,我使用后台应用程序刷新。我已经检查了背景模式的背景提取。当我点击模拟背景提取时,它会在后台刷新数据。但如果我的应用程序关闭并且我从服务器更新数据,我的应用程序不会更新数据。即使我等了很长时间(大约一个小时)并重新启动。请帮忙。 我的代码是:

-(void)application:(UIApplication *)application 
    performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler 
{
    NSLog(@"Fetch Started");
    [self getDataAtBAckground];
    completionHandler(UIBackgroundFetchResultNewData);
    NSLog(@"Fetch completed"); 
}

didFinishLaunchingWithOptions

- (BOOL)application:(UIApplication *)application 
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    [application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
    return YES;
}


-(void)getDataAtBAckground {

//-------Get All Categories-------//

expense = [[Expense alloc]init];

[expense deleteAllCategories];
[expense deleteAllChannels];

NSString *str = [NSString stringWithFormat:@"http://myServerAddress/categories.php"];
str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:str];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *error;
NSArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
if(error)
{
    NSLog(@"%@",[error localizedDescription]);
}
else
{
    NSArray *post = [json valueForKey:@"post"];
    NSArray *categNameArray  = [post valueForKey:@"name"];
    NSArray *categImageArray = [post valueForKey:@"image"];
    NSArray *categIdArray    = [post valueForKey:@"id"];

    for(int counter=0; counter<[categNameArray count]; counter++)
    {
        [expense addCategoryWithName:[categNameArray objectAtIndex:counter] image:[categImageArray objectAtIndex:counter] Catid:[categIdArray objectAtIndex:counter]];
    }
}
}

0 个答案:

没有答案