2016-06-07 46 views
0

这里我使用GET方法从服务器获取数据,但是当我最小化时,应用程序会在后台运行,并且在我重新打开后,我看不到这是我以前获取的任何数据,我只看到数据的第一次,当我最小化,然后重新打开这里迷路是我的代码使用“GET”方法获取数据当我从后台重新打开应用程序时丢失

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://198.178.4.9:89/mybite_joomla/cron/webapi.php?task=webapi.viewProfile"]cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
               timeoutInterval:10 
           ]; 


    [request setHTTPMethod:@"GET"]; 

    [request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"]; 

NSError *err; 
NSURLResponse *response; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; 

NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];{ 


for (NSDictionary *dict in jsonArray) { 

    nameTextView.text =[jsonArray valueForKey:@"name"]; 

    usernameTextView.text =[jsonArray valueForKey : @"username"]; 

    emailTextView.text=[jsonArray valueForKey : @"email"]; 

    cityTextView.text =[jsonArray valueForKey : @"city"]; 

    stateTextView.text =[jsonArray valueForKey : @"state"]; 

    countryTextView.text =[jsonArray valueForKey : @"country"]; 
} 
} 

回答

0

声明NSDictionary *jsonArray(nonatomic,strong)一个属性,使用该属性检索数据或者填写textFields。

第二件事,如果你的应用程序在后台保持很长时间并处于暂停状态,那么你应该必须将数据存储在数据库中。因为一旦app被暂停,它就会释放实例内存。

第二重要的是你不应该使用​​,因为它是未实现的。

由于Apple documentation状态,

指定不仅要在本地缓存中的数据被忽略,但代理和其他中间体应指示不顾缓存,只要该协议允许。 该常数未实现,不应使用。

所以,你可以通过0作为参数传递给cachePolicy或可以构造要求,如果timeoutInterval不是强制性等。

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://198.178.4.9:89/mybite_joomla/cron/webapi.php?task=webapi.viewProfile"]]; 
+0

我可以使用存储NSUserDefaults的会是在我的案件有帮助的数据,如果应用程序仍然在后台长时间处于暂停状态得到@Lion – vicky

+0

是的,你可以用'NSUserDefaults' – Lion

+0

并没有使用的声明NSDictionary * jsonArray作为属性...即使应用程序在后台进入几秒钟我失去了数据@Lion – vicky

相关问题