2012-06-06 16 views
5

के साथ आवृत्ति-सी ब्लॉक का उपयोग करके, इसलिए मैं किसी दिए गए उपयोगकर्ता के लिए सभी अनुयायियों की सूची पुनर्प्राप्त करने के लिए आईओएस 5 में निर्मित ट्विटर एपीआई का उपयोग करने की कोशिश कर रहा हूं। सभी उदाहरण प्रलेखन में मैं पा सकता हूं, जब अनुरोध लौटाता है तो एपीआई पास करने वाले इनलाइन ब्लॉक को निष्पादित करने के लिए अनुरोध किए जाते हैं, जो कि अधिकांश सरल सामग्री के लिए ठीक है, लेकिन जब मैं ~ 1000 अनुयायियों को प्राप्त करने की कोशिश कर रहा हूं, और अनुरोध उन्हें आकार ~ 100 में पर्जित कर रहा है, मैं वापस आने वाले 'अगली पेजिंग पते' का उपयोग करके फिर से अनुरोध को कॉल करने के लिए अटक गया हूं और पूर्णता ब्लॉक के अंदर संसाधित किया गया हूं। यहाँ कोड है:आईओएस ट्विटर एपीआई

- (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]; 
} 

आदर्श रूप में मैं किसी तरह के लिए इस डेटा लौटाए जाने के लिए सुनने चाहते हैं एक अगले पृष्ठ मूल्य के लिए जाँच करें और यदि वह मौजूद है, कोड ब्लॉक पुन: उपयोग और संलग्न डेटा नहीं दिया। मुझे यकीन है कि इसे हासिल करने के लिए 'सर्वश्रेष्ठ अभ्यास' तरीका होना चाहिए, किसी भी मदद की बहुत सराहना की जाएगी!

+0

ऑफटॉपिक: क्या आप "अनुयायियों" का अनुरोध करते हैं जो अनुरोध में उपयोग करते हैं RequestWithHandler विधि ivar? – CarlJ

+0

हां, यह एक 'एनएसएआरएआरई –

+0

है और आपने इसे कहां परिभाषित किया? हेडर फ़ाइल में – CarlJ

उत्तर

3

@Eimantas 'जवाब पर विस्तार करने के लिए, आपके अनुरोध हैंडलर एक विशिष्ट ब्लॉक हस्ताक्षर की उम्मीद है, तो आप पृष्ठ संख्या को संभालने के लिए एक अलग तरह की जरूरत है।

-(void)getTwitterFollowers { 
    // set up request... 
    __block int page = 0; 
    __block void (^requestHandler)(NSData*, NSHTTPURLResponse*, NSError*) = null; 
    __block TWRequest* request = [[TWRequest alloc] initWithURL:url 
                parameters:params 
                requestMethod:TWRequestMethodGET]; 
    requestHandler = [^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) { 
     followers = [NSJSONSerialization JSONObjectWithData:responseData 
              options:NSJSONReadingMutableLeaves 
              error:&jsonError];    
     if (followers != nil) {       
      // process followers 
      page++; 
      NSMutableDictionary *params = [NSMutableDictionary dictionaryWithDictionary:request.parameters]; 
      // update params with page number 
      request = [[TWRequest alloc] initWithURL:url 
              parameters:params 
             requestMethod:TWRequestMethodGET]; 
      [request performRequestWithHandler:requestHandler]; 
     } 
    } copy]; 

    // now call the block for the first page 
    [request performRequestWithHandler:requestHandler]; 
} 
5

किसी भी ब्लॉक को दोबारा उपयोग करने के लिए आपको इसे पहले घोषित करना होगा और इसे बाद में परिभाषित करना होगा। इस प्रयास करें:

__block void (^requestPageBlock)(NSInteger pageNumber) = NULL; 

requestPageBlock = [^(NSInteger pageNumber) { 
    // do request with some calculations 
    if (nextPageExists) { 
     requestPageBlock(pageNumber + 1); 
    } 
} copy]; 

// now call the block for the first page 
requestPageBlock(0); 
+3

आपको घोषित करने की आवश्यकता नहीं है यह पहले लेकिन मुझे विश्वास है कि आपको ब्लॉक वैरिएबल को __block के रूप में निर्दिष्ट करने की आवश्यकता है और ब्लॉक को अपने आप को संदर्भित करने से पहले स्टैक पर कॉपी करें। अन्यथा आपको एक EXC_BAD_ACCESS मिलेगा। तो .. '__block शून्य (^ अनुरोध) (NSUInteger) = [^ (NSUInteger पेज) {..... कोड ......} प्रतिलिपि];' –

+1

आप सही हैं! फिक्स के लिए धन्यवाद। – Eimantas

+2

कोई समस्या नहीं है। मैंने इसे समझने में थोड़ा सा समय बिताया। साथ ही, यदि आप किसी अन्य ब्लॉक को रिकर्सिव ब्लॉक के भीतर से संदर्भित करते हैं तो बहुत सावधानी बरतें। आप इसे अपने उदाहरण में नहीं करते हैं, लेकिन यह एक छोटा कदम दूर है और यह मेमोरी लीक का कारण बन सकता है .... यहां देखें: http://stackoverflow.com/a/8896766/1147934 –

संबंधित मुद्दे