Domanda

Ho un metodo come questo. Funziona bene quando il mio dispositivo è collegato attraverso la rete wi-fi, ma quando è collegato tramite la rete 3G si blocca la mia app per alcuni secondi. Quindi, perché è un app interattivo, quando lo fa un po 'diverso richiesta Inserisci deve continuare in esecuzione in modo che l'utente può continuare a utilizzare l'applicazione. Qualsiasi soluzione per questo?

Ho provato a ridurre il [theRequest setTimeoutInterval: 2.0]; ma che non ha risolvere il mio problema.

// post request
    - (void)postRequestWithURL:(NSString *)url 
                                body:(NSString *)body 
                         contentType:(NSString *)contentType 
                             options:(NSDictionary *)dict
    {
        // set request
        NSURL *requestURL = [NSURL URLWithString:url];
        NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];


        if([dict count] > 0)
        {
            for (id key in dict) {
                NSLog(@"[theRequest addValue:%@ forHTTPHeaderField:%@]", [dict valueForKey:key], key);
                [theRequest addValue:[dict valueForKey:key] forHTTPHeaderField:key];
            }
        }

        if (contentType != nil) {
            [theRequest addValue:contentType forHTTPHeaderField:@"Content-type"];
        }

        [theRequest setURL:requestURL];
        [theRequest setTimeoutInterval:2.0];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding]];
        [self.oauthAuthentication authorizeRequest:theRequest];
        // make request
        //responseData = [NSURLConnection sendSynchronousRequest:theRequest 
        //                                   returningResponse:&response 
        //                                               error:&error]; 

        NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
        self.web = conn;

        [conn release];

        NSLog(@"########## REQUEST URL: %@", url);




        // request and response sending and returning objects
    }
È stato utile?

Soluzione

Si gela la vostra applicazione perché si hai detto a. Hai superato SI in startImmediately. Ciò significa che inizierà la connessione su quel thread e attendere finché non è finito. Credo che il filo si sta facendo questo sul sarà il filo conduttore - quello che gestisce anche l'interfaccia utente, ecc:)

È necessario usare qualcosa di simile connectionWithRequest:delegate: - questo verrà eseguito la richiesta in background e ti dirà quando è fatto

.

PS Il motivo non è stato posto il bug in wifi è perché i dati sono stati inviati così veloce non si poteva notare la pausa nella vostra applicazione:)

PPS Il motivo per il timeout non risolvere il problema è perché la richiesta non è stata cronometrando fuori - è stato solo ottenere dati molto lentamente:)


Modifica

Qualcosa di simile a questo:

self.web = [NSURLConnection connectionWithRequest:request delegate:self];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top