0

次のコードは私の UI をフリーズしています。アクションを実行できません。

- (void) longPoll {
    //create an autorelease pool for the thread
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSError* error = nil;
        NSURLResponse* response = nil;
        NSURL* requestUrl = [NSURL URLWithString:@"myurl"];
        NSURLRequest* request = [NSURLRequest requestWithURL:requestUrl];

        //send the request (will block until a response comes back)
        NSData* responseData = [NSURLConnection sendSynchronousRequest:request
                                                     returningResponse:&response error:&error];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self dataReceived:responseData];
        });
    });
        //compose the request


        //pass the response on to the handler (can also check for errors here, if you want)


        //clear the pool

    }




- (void) startPoll {
    //not covered in this example:  stopping the poll or ensuring that only 1 poll is active at any given time
    [self performSelectorInBackground:@selector(longPoll) withObject: nil];
}

- (void) dataReceived: (NSData*) theData {
    //process the response here
    NSDictionary *dict=[theData JSONValue];
   [self ParseJson:dict];
     [self performSelectorInBackground:@selector(longPoll) withObject: nil];
}

誰かがその正確な理由、またはポーリングを継続するために同様のコードを実行するための代替手段を教えてもらえますか?

4

2 に答える 2

1

無限ループを作成しています:

longCallコールdataReceivedコールlongCallなど....

于 2013-07-24T10:24:45.677 に答える
0

まさにあなたがしたいこと。longPool と dataReceived の間に無限ループがあります。この呼び出しを停止して使用できるメカニズムが必要です。

@autorelease {} block for create autorelease pool in ARC Enabled project and
NSAutoReleasePool class obj for Without ARC.
于 2013-07-24T11:37:04.257 に答える