2017-03-24 7 views
0

なぜ完了ブロックが機能しないのか分かりません。最初に私は現在の場所を見つけて、サーバーからデータを取得するために緯度と経度を使用します。 NSURLSessionDelegateを使用してバックグラウンドSessionを試しましたが、データも取得しません。今日の内線番号内でUrlsessionを使用しています

- (void)widgetPerformUpdateWithCompletionHandler:(void (^)(NCUpdateResult))completionHandler { 
    // Perform any setup necessary in order to update the view. 

    [self updateWidget]; 
    self.placeImageView.image = [UIImage imageNamed:@"no-image"]; 
    self.crowdednessLevelImageView.image = [UIImage imageNamed:@"circle-crowd"]; 
    self.placeImageView.layer.cornerRadius = 10; 

    NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.wherepeeps.mostCrowdedPlace.widgert"]; 
    NSString *coordinateString = [defaults objectForKey:@"CurrentLocationCoordinateString"]; 
    NSDictionary *responseObject = [defaults objectForKey:@"listOfPlacesForWidget"]; 
    NSLog(@"responseObject - %@", responseObject); 
    if (coordinateString) { 
     NSString *stringUrl = [NSString stringWithFormat:@"https://sheltered-fjord-29765.herokuapp.com/api/v1/search?location=%@", coordinateString]; 
     NSURLSession *mySession = [self configureMySession]; 
     NSURL *url = [NSURL URLWithString:stringUrl]; 
     [mySession dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 
      NSDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; 
      NSUserDefaults *defaults = [[NSUserDefaults standardUserDefaults] initWithSuiteName:@"group.wherepeeps.mostCrowdedPlace.widgert"]; 
      [defaults setObject:responseObject forKey:@"listOfPlacesForWidget"]; 
      [defaults synchronize]; 
      NSLog(@"responseObject - %@", responseObject); 
      completionHandler(NCUpdateResultNewData); 
     }];  

    } else { 
     completionHandler(NCUpdateResultNoData); 
    } 
} 

- (NSURLSession *)configureMySession { 
    if (!self.mySession) { 
     // NSURLSessionConfiguration* config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"com.mycompany.myapp.backgroundsession"]; 
     NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
     // To access the shared container you set up, use the sharedContainerIdentifier property on your configuration object. 
     config.sharedContainerIdentifier = @"com.mycompany.myappgroupidentifier"; 
     self.mySession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; 
    } 
    return self.mySession; 
} 
+0

'dataTaskWithURL:'は、 'resume'を呼び出さなければならないタスクを返します。 – dan

答えて

0

persormSelectorOnMainThreadを使用してメインスレッドでNsurlsession要求を呼び出してみてください。

関連する問題