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 8484417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:22:32+00:00 2026-06-10T20:22:32+00:00

How do I keep an object in scope, which performs a task in background,

  • 0

How do I keep an object in scope, which performs a task in background, that was instantiated from a method of another object?

See the following example. The display method of object A instantiates object B and calls its display method. When the B object’s display method, starts an asynchronous task, control goes back to A::display method, which returns, and bObject gets deallocated. When bObject’s data/method are accessed in the background thread, this will throw exception.

What is the best way of maintaining the scope for object B? I can declare the object B as a member variable for A, but this will increase the scope for the entire lifespan of object A.

 @implementation A

   -(void) display
   {
     B* bObject = [[B alloc] init];
     [bObject display];
   }
@end

 @implementation B

   -(void) display
   {
      dispatch_async((dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_LOW, 0),{
       self.data = 5;
       ---------
     });
@end
  • 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-10T20:22:33+00:00Added an answer on June 10, 2026 at 8:22 pm

    Previously in iOS you would be able to do this and then assuming your object created returned with some delegate callback you could release it. Now you will have to make “bObject” a member variable for your class and either set it to nil when you are done with it or wait for A to be destroyed which will put bObject out of scope. I’m assuming this is with ARC and as soon as “display” finishes your local variable is out of scope and is deallocated because of ARC.

    you can add this to A:

    @property (nonatomic, strong)B *bObject;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I keep reading that object-oriented programming can basically be done in any programming language,
I have a 3d object which I want to keep in the center of
For a remoting framework, I need to keep some metadata about object that I'm
Have any ways to keep a variables (object) in a page scope? I need
How do I keep data associated with an MKAnnotation object after the user taps
I have an options object in my CS class, and I'd like to keep
I keep hearing that Android applications should try to limit the number of objects
I would like to this in the use case of which I can keep
I have the following query that shows the number of participants in the last
If you call a constructor from a within a using statement the object is

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.