zapolnov/nikolay-zapolnov-odesk-ios-test — pull request #1 by zapolnov (2014-03-23 17:29:00+00:00)
Title: Fix

What was the root cause of the bug #1?

Application called dispatch_sync into main queue while being in the main thread. Request was put into the queue of the main thread, but for it to be processed we have to return to the event loop. But dispatch_sync was waiting (indefinitely) for event to be processed.

How did you fix the problem?

Because method is called from the main thread, there is no need to use GCD’s functions. But initial intent of the author of this method is not clear. Maybe it is supposed to be called from other threads. I have replaced dispatch_sync with dispatch_async. This guarantees that message will be written into the log from the main thread. It is not clear whether it is correct - dispatch_sync could be there for some reason. But there is no additional information about the method (no comments) and from the code of the application this fix seems ok to me.

What was the root cause of the bug #2?

Code of the secondBug: method uses Objective-C block to print value of variable ‘x’ into the log. But the way it is written, it captures value of ‘x’ at the moment when block is created. When block is called, old value of ‘x’ is written into the log.

How did you fix the problem?

I have added the ‘__block’ modifier to the declaration of variable ‘x’. This tells to compiler that variable ‘x’ should be captured by reference instead of value. After that, value 124 is printed into the log, as expected.

What was the root cause of the bug #3?

Code of the thirdBug: method uses NSLog with the %@ format specifier to print contents of NSArray. In this case, method ‘description’ of NSArray is being called. In this method NSArray builds a string containing a description of each element in NSArray. Description for each element is obtained using the ‘description’ method of that element. In the provided program, class ‘ModelsEntity’ (a class for entries of NSArray being written into the log) does not have a ‘description’ method. In this case default implementation of this method (from NSObject) is used which gives long hard-to-read strings.

How did you fix the problem?

I have implemented method ‘description’ in class ModelsEntity to return description in the format requested in this document.

What was the root cause of the bug #4?

In the code of the fourthBug: method there is a static variable ‘count’, initialized into 1. After that there is a call to [CoreDataHelpers cleanData] but only if ‘count’ is greater than 1. Values of static variables are preserved between calls to a function or method. When this method is called for the first time, the value of ‘count’ will be 1 and the ‘cleanData’ method will not be called. This seems to be correct, because we are not allowed to call this method when we have no data. But, we actually have data there when we start application for the second time. In this case, method ‘fillUnsortedData’ appends the same data to the end of the previously saved data, resulting into duplication of the data.

How did you fix the problem?

I have removed the static variable check and have moved the call of the ‘cleanData’ method to the end of the fourthBug: method. So we are cleaning data after using it. This could be an incorrect way of fixing the bug (depends on the use case of the application - maybe we have to preserve this data for future runs). But in the context of this task this fix seems ok.

What was the root cause of the bug #5?

There are no sort descriptors in NSFetchRequest when fetching data.

How did you fix the problem?

I have added method arrayForFetchRequestWithName:sorted: into the CoreDataHelpers class. When sorted: set to YES, this method adds sort descriptor to the NSFetchRequest instance. In this case fetched data will be sorted.