Also versuche ich die integrierte Twitter-API in iOS 5 zu verwenden, um eine Liste aller Follower für einen bestimmten Benutzer abzurufen. In der gesamten Beispieldokumentation, die ich finden kann, werden Anfragen an die API gestellt, die Inline - Blöcke weiterleitet, die ausgeführt werden, wenn die Anfrage zurückkehrt, was für die meisten einfacheren Sachen gut ist, ABER wenn ich versuche, ~ 1000 Follower zu bekommen Anfrage gibt sie in Größe ~ 100 ausgelagert zurück, ich bin fest, wie rekursiv die Anfrage erneut aufrufen mit der 'nächsten Paging-Adresse' zurückgegeben und verarbeitet im Abschlussblock. Hier ist der Code:Verwenden von Objective-C-Blöcken rekursiv mit iOS Twitter-API
- (void)getTwitterFollowers {
// First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType =
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user for access to his Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(@"User rejected access to his account.");
}
else {
// Grab the available accounts
NSArray *twitterAccounts =
[store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicity
ACAccount *account = [twitterAccounts objectAtIndex:0];
// Now make an authenticated request to our endpoint
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"1" forKey:@"include_entities"];
// The endpoint that we wish to call
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/followers.json"];
// Build the request with our parameter
request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodGET];
[params release];
// Attach the account object to this request
[request setAccount:account];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if (!responseData) {
// inspect the contents of error
FullLog(@"%@", error);
}
else {
NSError *jsonError;
followers = [NSJSONSerialization JSONObjectWithData:responseData
options:NSJSONReadingMutableLeaves
error:&jsonError];
if (followers != nil) {
// THE DATA RETURNED HERE CONTAINS THE NEXT PAGE VALUE NEEDED TO REQUEST THE NEXT 100 FOLLOWERS,
//WHAT IS THE BEST WAY TO USE THIS??
FullLog(@"%@", followers);
}
else {
// inspect the contents of jsonError
FullLog(@"%@", jsonError);
}
}
}];
} // if ([twitterAccounts count] > 0)
} // if (granted)
}];
[store release];
}
Im Idealfall würde ich irgendwie gerne hören für diese Daten zurückgegeben werden, überprüfen Sie für eine nächste Seite Wert und wenn es vorhanden ist, wiederverwenden den Codeblock und fügen die Daten zurückgegeben. Ich bin mir sicher, dass es einen Best-Practice-Weg geben muss, um dies zu erreichen, jede Hilfe wäre sehr willkommen!
offtopic: ist die "Follower", die Sie in der Anfrage performRequestWithHandler Methode eine iVar verwenden? – CarlJ
Ja, es ist ein 'NSArray' –
und wo hast du es definiert? – CarlJ