Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:38:55+00:00 2026-06-17T12:38:55+00:00

I am creating a NSObject to load the TimeLine twitter Code: + (NSArray *)executeTweetFetch

  • 0

I am creating a NSObject to load the TimeLine twitter
Code:

+ (NSArray *)executeTweetFetch

{

    __block NSArray *fetchedTweets = [NSArray array];

    NSURL *getTweetUrl = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/home_timeline.json"];



    SLRequest *tweetRequest  = [SLRequest requestForServiceType:SLServiceTypeTwitter

                                                  requestMethod:SLRequestMethodGET

                                                            URL:getTweetUrl

                                                     parameters:nil];
    tweetRequest.account = ACAccount HERE!!!;

    [tweetRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

        if ([urlResponse statusCode] == 200) {
            // Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.

            NSError *jsonParsingError = nil;
            fetchedTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
            //At this point, fetchedTweet seems working fine, it gets the array send back.
        }
    }];

    return fetchedTweets;

}

…is Ok…but I have to add a ACAccount (another view). and since it is a + (NSArray *) it does not recognize my (ACAccount) or any object outside

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:38:56+00:00Added an answer on June 17, 2026 at 12:38 pm

    Synchroneity

    Before getting into the issue that you’re asking about, it’s well worth discussing what @MartinR mentioned at greater length: The method -[ACAccount performRequestWithHandler:] is asynchronous.

    Calling the method from the thread that code is being executed on results in some activity starting up on a second background queue. Meanwhile, execution continues on the first thread (the thread that is executing performRequestWithHandler: and executeTweetRequest). Some indeterminate amount of time after executeTweetRequest is called, the block passed as the only argument to -[ACAccount performRequestWithHandler:] is called.

    Consequently, you will not be able to synchronously return the array of tweets from executeTweetRequest.

    The most stylish approach would be to change your method to be block based:

    + (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block
    {
        NSURL *getTweetUrl = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/home_timeline.json"];
        SLRequest *tweetRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                     requestMethod:SLRequestMethodGET
                                                               URL:getTweetUrl
                                                        parameters:nil];
        tweetRequest.account = //...this will be addressed in a moment
        [tweetRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            if ([urlResponse statusCode] == 200) {
                NSError *jsonParsingError = nil;
                NSArray *fetchedTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
                block(fetchedTweets, jsonParsingError);
            }
        }];
    }
    

    When calling fetchTweetsWithCompletion: from your second UITableViewController subclass, you will pass in a block probably much like the following:

    - (void)viewDidLoad
    {
        //...
        [TwitterWrapper fetchTweetsWithCompletion:^(NSArray *tweets, NSError *error) {
            if (tweets) {
                self.tweets = tweets;
                [self.tableView reloadData];
            } else if (error) {
                //present a UIAlertView, perhaps...
            }
        }];
        //...
    }
    

    The result of this code is that once the fetch of the tweets on a background queue has completed, your table view controller’s property tweets will be set to the fetched result and its table view will reload its data.

    Scope (Addressing The Question)

    The issue that you’re describing

    since it is a + (NSArray *) it does not recognize my (ACAccount) or any object outside

    is that you’re unable to access the instance variables of any of the instances of your class (I’m going to call it TwitterWrapper for convenience) from within your class method +[TwitterWrapper executeTweetFetch]. The problem here is one of scope. (As a result, thanks to ARC, the problem is also one of memory management.)

    Our goal is to stash an instance of ACAccount somewhere from your first table view controller and access that instance from your second table view controller.

    There are several ways of doing this:

    Use A Global Variable

    The worst approach is to a use a dreaded global variable:

    In TwitterWrapper.h, you would declare extern the ACAccount for twitter:

    //TwitterWrapper.h
    
    extern ACAccount *twitterAccount;
    
    @interface TwitterWrapper : executeTweetFetch
    + (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block;
    @end
    

    In TwitterWrapper.m, you would define twitterAccount so that it has global scope (for example, prior to the @implementation block:

    ACAccount *twitterAccount;
    
    @implementation TwitterWrapper
    //...
    @end
    

    And in the definition of your class method executeTweetFetch you would have

    + (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block
    {
        //...
        tweetRequest.account = twitterAccount;
        //...
    }
    

    NOTE: This approach is very unstylish, not to mention outright dangerous. Global variables should be avoided whenever possible. Avoiding one is certainly possible in this case.


    The next two approaches both involve changing fetchTweetsWithCompletion: into an instance method and adding an ACAccount property to TwitterWrapper. The class’ @interface will look like

    @interface TwitterWrapper : NSObject
    @property (nonatomic) ACAccount *account;
    - (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block;
    @end
    

    The @implementation will look like

    @implementation TwitterWrapper
    
    - (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block
    {
        //...
        twitterRequest.account = self.account;
        //...
    }
    
    @end
    

    Use A Shared Instance

    The second worst approach in this case is to use a shared instance of TwitterWrapper. This involves adding a class method (called something clever like +sharedWrapper) to the class:

    @interface TwitterWrapper : NSObject
    + (TwitterWrapper *)sharedWrapper;
    @property (nonatomic) ACAccount *account;
    - (void)fetchTweetsWithCompletion:(void (^)(NSArray *, NSError *))block;
    @end
    

    and using Grand Central Dispatch‘s dispatch_once to initialize a static local variable:

    @implementation TwitterWrapper
    
    //...
    
    + (TwitterWrapper *)sharedWrapper
    {
        static TwitterWrapper *sharedWrapper = nil;
    
        static dispatch_once_t pred;
        dispatch_once(&pred, ^{ 
            sharedWrapper = [[self alloc] init]; 
        });
    
        return sharedWrapper;
    }
    
    //...
    
    @end
    

    At this point, you have an instance of TwitterWrapper into which you can store the instance of ACAccount from your first table view controller and make use of (via self.account within the implementation of -fetchTweetsWithCompletion:) from your second table view controller:

    Somewhere within your first UITableViewController subclass you would have

    //...
    ACAccount *theAccount = //this is initialized somehow within this class (the first UITableViewController subclass), probably by the selection of a row
    [[TwitterWrapper sharedWrapper] setAccount:theAccount];
    //...
    

    Then, within your second UITableViewController subclass (probably within -viewDidLoad), you would have

        //...
        [[TwitterWrapper sharedWrapper] fetchTweetsWithCompletion:^(NSArray *tweets, NSError *error) {
            if (tweets) {
                self.tweets = tweets;
                [self.tableView reloadData];
            } else if (error) {
                //present a UIAlertView, perhaps...
            }
        }];
        //...
    

    This solution, however, is very similar to using a global variable. (The difference is that the creation of the global variable will be thread safe.)

    Pass An Instance Of TwitterWrapper

    A much better solution is to pass an instance of TwitterWrapper from your first table view controller to your second.

    NOTE: For brevity, I will assume that the second table view controller becomes active immediately after the user selects an account row in the first table view controller.

    To do this, you will need to add a property

    @property (nonatomic) TwitterWrapper *twitterWrapper;
    

    to your second UITableViewController subclass.

    Somewhere within your first table view controller you would have

    //...
    ACAccount *theAccount = //this is initialized somehow within this class (the first UITableViewController subclass), probably by the selection of a row
    TwitterWrapper *theTwitterWrapper = [[TwitterWrapper alloc] init];
    twitterWrapper.account = theAccount;
    SecondTableViewController *tweetsTableViewController = //initialize the table view controller
    tweetsTableViewController.twitterWrapper = theTwitterWrapper;
    //...    
    

    Then, within your second UITableViewController subclass (probably within -viewDidLoad), you would have

        //...
        [self.twitterWrapper fetchTweetsWithCompletion:^(NSArray *tweets, NSError *error) {
            if (tweets) {
                self.tweets = tweets;
                [self.tableView reloadData];
            } else if (error) {
                //present a UIAlertView, perhaps...
            }
        }];
        //...
    

    Pass An Instance Of ACAccount

    Rather than storing an instance of TwitterWrapper on your second UITableViewController subclass instance, the best solution is to store an instance of ACAccount and alter TwitterWrapper‘s interface for fetching tweets once again.

    In this case, we’ll want the fetch method to again be a class method. As @MartinR suggested, add an account parameter to your fetch method:

    + (void)fetchTweetsWithAccount:(ACAccount *)theAccount completion:(void (^)(NSArray *, NSError *))block
    {
        NSURL *getTweetUrl = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/home_timeline.json"];
        SLRequest *tweetRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                     requestMethod:SLRequestMethodGET
                                                               URL:getTweetUrl
                                                        parameters:nil];
        tweetRequest.account = theAccount;
        [tweetRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            if ([urlResponse statusCode] == 200) {
                NSError *jsonParsingError = nil;
                NSArray *fetchedTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
                block(fetchedTweets, jsonParsingError);
            }
        }];
    }
    

    Next, add a property of type ACAccount to your second UITableViewController subclass:

    @property (nonatomic) ACAccount *account;
    

    Then, somewhere within your first table view controller you would have

    //...
    ACAccount *theAccount = ////this is initialized somehow within this class (the first UITableViewController subclass), probably by the selection of a row
    SecondTableViewController *tweetsTableViewController = //initialize the table view controller
    tweetsTableViewController.account = theAccount;
    //...    
    

    Then, within your second UITableViewController subclass (probably within -viewDidLoad), you would have

        //...
        [TwitterWrapper fetchTweetsWithAccount:self.account completion:^(NSArray *tweets, NSError *error) {
            if (tweets) {
                self.tweets = tweets;
                [self.tableView reloadData];
            } else if (error) {
                //present a UIAlertView, perhaps...
            }
        }];
        //...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I declare an NSArray in one class like this: .h @interface HTTP : NSObject
I have a NSArray of Foo objects. @interface Foo : NSObject { } -
I have the current problem: From an NSObject i'm creating / loading and animating
I am creating a iOS plugin for unity3D. below is the code. How ever
Greetings, I'm looking at Matt Gallagher's macro for creating singleton classes. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html Basically, I
I'm creating a NSTextView in my AppController.h: @interface AppController : NSObject { IBOutlet NSTextView
I am starting macruby and I am following this tutorial: http://blog.phusion.nl/2010/03/12/creating-our-very-first-mac-application-with-ruby-how-exciting/ I am stuck
I've just been looking at the header files for NSObject, and I'm creating a
I am creating a program that requires me to build an NSMutable array and
I've got some code like this: @interface MyTimer : NSObject - (int)getValue; @end @interface

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.