A
A
Anton Ivanov2018-08-15 20:04:09
Objective-C
Anton Ivanov, 2018-08-15 20:04:09

Why is a variable removed in objective C?

Hello.
I have little experience in iPhone programming, so the answer can be quite simple, but several hours of googling and stackoverflow did not bring the desired result.
The application uses the library and works as follows.
Calling a method from a library. This method makes a request to the API and returns the result. The application then works with this result.
The result is also cached in the library.
Here are code examples:
calling a method from a lib

[[DataCache sharedInstance] getData:accessKey onSuccess:^(DataStruct *dataStruct) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [dataStruct logUrl]; // метод из одной строки:  NSLog(@"url: %@", _url);

             // всякий другой код
        });
    } onError:^{
    }];

this is how the result is cached in lib:
// в конструкторе класса DataCache
           _mediaInfoCache = [[NSMutableDictionary alloc] init];

// в функции получения данных
            NSMutableDictionary *record = [[NSMutableDictionary alloc] init];
            [record setObject:dataStruct forKey:@"dataStruct"];
            [record setObject:[NSDate date] forKey:@"date"];
            [_mediaInfoCache setObject:record forKey:targetName];

DataStruct is inherited from NSObject
All fields are written as nonatomic, strong
The essence of the problem is that in about 90% of cases when entering the class instance function that performs NSLog, we get garbage in the instance fields and crash.
10% is ok.
Either compiled without ARC. Program with it
Observations:
- If you execute NSLog not from the class instance function (logUrl), but log directly from the main block, then everything is ok.
- If you debug, then the problem occurs very rarely, somewhere around 1 time out of 20-30. It is not clear how to repeat 100% under debugging.
- If you remove dispatch_async, then everything almost works (read the next paragraph)
- 100% does not work when you re-request, when the data is taken from the cache.
- If you take the methods out of the lib and use them in the main application, then everything works fine.
The question is where to look? What other details are needed?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question