Keep object alive in memory for another class

Asked

Viewed 148 times

1

Hello! I have a very simple doubt, because I have already found a solution for it, but it still seems problematic. I will explain:

I created a connection with web service to receive some data, and at the end I am storing this data in a dictionary, which in turn is saved in an array.

However, I need this array to be initialized elsewhere, within the class that will save the data to sqlite. But I don’t know how to initialize this array with the data that was received from the web service.

The solution I found was to use the Appdelegate. Appdelegate would have a variable that will receive this array, and then I would generate an instance of Appdelegate within the class that manages the sqlite database. However, I believe that depending on the amount, and the volume of data in the tables, I could overload the Appdelegate, consuming a lot of memory.

Would anyone know how to solve this problem!

Thank you.

1 answer

3


Its solution is valid (using Appdelegate) but it is bad from the point of view of object orientation. You will be placing on Appdelegate an attribute that is not logically related to that class. I suggest using the same idea (Singleton), but creating a specific object for the data shared between your classes. This way you decrease the code coupling and increase its cohesion. Example:

@interface SharedData : NSObject

+ (SharedData*)sharedInstance;
@property(nonatomic, copy) NSArray *nodes;

@end

@implementation SharedData

+ (SharedData *)sharedInstance
{
    static SharedData *_sharedInstance = nil;

    static dispatch_once_t instanceOnceToken;
    dispatch_once(&instanceOnceToken, ^{
        _sharedInstance = [[SharedData alloc] init];
    });

    return _sharedInstance;
}
@end

In other classes you access Singleton properties and methods in a similar way as you would with Appdelegate, i.e., through the shared instance, e.g.:

[SharedData sharedInstance].nodes = [NSArray new];

As for the memory issue, this solution is slightly less efficient because it allocates an additional object, but this will not impact performance. Problems may appear if you keep large and complex objects in memory, such as bitmaps. With dictionaries, you will hardly have problems.

  • So, using the example, I would have to instantiate Shareddata at one point to receive the data I want, and then the moment I instantiate it again at another point, and it will have in its attribute the data I passed previously? And as for the use of memory, I may need to worry, because in this data that I will receive through the web service I will have some images. I’ll check the size of them with the head of DB.

  • 1

    Yes, the entire object will be kept in memory during the application lifecycle. You don’t have to worry about allocation. It will occur the first time Singleton is accessed.

  • Well, I made a more objective solution to the problem. I modified the class method that manages the database. Making the method receive the data and move to the input function. Then I prompt a Bdmanager object and send the insertData method with the properties(which are the values received from the webservice) as arguments. And the insertion works 100% within the method!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.