У меня есть приложение, которое имеет UITabelView
, которое получает контент со стороны сервера. В моем приложении я считываю контент с сервера каждые 30 секунд .... для этого я использую NSTimer
.Это NSTimer
инициализируется, когда я загружаю представление, которое содержит UITableView
, и становится недействительным, когда я покидаю это представление.
Моя проблема заключается в следующем:
, если на стороне серверасодержимое для UITableView
обновляется новым элементом, а JSON
, полученный в приложении iphone в ответ на запрос к серверу, содержит этот элемент .... UITableView
, отображаемый на экране, все еще не обновлен.
Вот как я это сделал:
// таймер запускается при загрузке этого представления и вызывается метод repeatServerRequest
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if(playlistTimer == nil)
playlistTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target: self selector: @selector(repeatServerRequest) userInfo: nil repeats: YES];
}
//метод repeatServerRequest запускает новый поток в фоновом режиме, который // выполняет запрос к серверу на загрузку контента
- (void) repeatServerRequest{
[NSThread detachNewThreadSelector:@selector(backgroundThinking) toTarget:self withObject:nil];
}
- (void) backgroundThinking{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:@"a link to server"];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDelegate:self];
[request startAsynchronous];
[pool release];
}
///when the response from server comes in these methods are called:
- (void)requestFinished:(ASIHTTPRequest *)request
{
[self performSelectorOnMainThread:@selector(didFindAnswer:) withObject:request waitUntilDone:YES];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
NSError *error = [request error];
NSLog(@"the value of error %@", error);
}
- (void) didFindAnswer:(ASIHTTPRequest *) request{
NSLog(@"update tabel");
SBJSON *parser = [[SBJSON alloc] init];
NSString *responseString = [request responseString];
NSArray *statuses = [parser objectWithString:responseString error:nil];
streams = [statuses valueForKey:@"_playLists"];
[parser release];
playList = [[NSMutableArray alloc] init];
idList = [[NSMutableArray alloc] init];
int ndx;
for (ndx = 0; ndx<streams.count; ndx++) {
NSDictionary *stream = (NSDictionary *)[streams objectAtIndex:ndx];
[playList addObject:[stream valueForKey:@"name"]];
[idList addObject:[stream valueForKey:@"id_playlist"]];
}
NSLog(@"playList %@", playList);
oneview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 480, 320)];
tableViewPlaylist =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
tableViewPlaylist.bounces=NO;
tableViewPlaylist.backgroundColor=[UIColor clearColor];
[tableViewPlaylist setDelegate:self];
[tableViewPlaylist setDataSource:self];
}
Так что, когда я обновляю контент на стороне сервера, JSON, который я получаю в ответ насерверная часть обновлена, но UITAbelView нет, UNLESS I RUN AGAIN MY APP
. Есть идеи, почему?