Fix for App Crashing Issue caused by Google Analytics Integration in iOS

Fix for App Crashing Issue caused by Google Analytics Integration in iOS
COMMENTS ()
Tweet

Hi Guys

In this post I’ll tell you how to fix an exception that you might have seen when integrating the Google Analytics library in your iOS app, which causes the app to crash.

I recently encountered this error myself when working on an iOS app and after a lot of R&D I finally managed to resolve it. I thought I’d share my findings, so that other iOS developers don’t get bogged down by the same issue.

When working my most recent iOS app, when I integrated the Google Analytics library, my app suddenly started to crash and started showing memory warnings on some devices. After I did some digging, I found out that this issue was caused by my use of multiple Managed Objects and the Google Analytics library that I had integrated in the app.

The Google Analytics library for iOS uses the core data library and has its own Managed Object context. This Managed Object context was dumping data in SQLite from time to time. This was the reason why I got the “NSManagedObjectContextDidSaveNotification” notification from NSManagedObjectContext.

Since I had added an observer for this notification in my app (see code snippet below), as I was using multiple Managed Objects in order to support multi-threading.

NSManagedObjectContextDidSaveNotification fix iOS 7

This “NSManagedObjectContextDidSaveNotification” was capturing all save notifications of Managed Objects. The implementation of the “loadManagedObjectFromNotification” function that I had defined in the selector was as follows:

NSManagedObjectContextDidSaveNotification fix iOS 7

However if you notice, there is a major flaw in the above function. And that is that this function will be called every time whenever any Managed Object within the application is saved; even if it’s the Managed Object from the Google Analytics library.

So the problem basically was that whenever the Google Analytic Managed Object was saved, if the Managed Object of my core data was busy in some read write operation the application crashed.

Solutions – NSManagedObjectContextDidSaveNotification fix iOS 7

After much R&D I was able to find two different approaches for the NSManagedObjectContextDidSaveNotification fix iOS 7

Solution 1:

If you read the tutorial provided by Google in their “Google Analytics SDK – Getting Started”, you’ll notice the following text:

NSManagedObjectContextDidSaveNotification fix iOS 7

When I was implementing Google Analytics, I just ignored this line. That was an oversight on my part. Since this little tip is a lifesaver for when your application starts to crash.

So what does this warning really mean? It basically means that you should filter your core data notifications. Normally, we don’t filter the notifications we add within the app.

As you know, each addObserver function has four parameters, in which we provide first three and leave the last one as “nil”. This last parameter is the optional ‘filter’. You can use this parameter to pass any object with the notification.

The receiver of the notification will then be able to access that object. So if you provide a Managed Object to the addObserver function, it will solve the app crashing issue. Code wise, I created the following saveContextmethod in order to save the Context and implement it, as given below:

NSManagedObjectContextDidSaveNotification fix iOS 7

If you notice in the above code, I have added the addObserver and removeObserver functions, both of which have the object parameter. This will help our Class to respond only to those notifications that are raised by mObjectContext while ignoring Google Analytics notifications.

Solution 2:

The other solution I found for this problem is much easier. That just involves placing a check within your notification function in order to ensure that it is not triggered by Google Analytics. You can do so by adding a simple if condition on top of your selector, as shown in the code snippet below:

NSManagedObjectContextDidSaveNotification fix iOS 7

Explore our blog

As a leading iPhone and Android app development company, Folio3 specializes in native app development services and cross platform mobile app development services (for both enterprise and consumer oriented apps) for iOS, Android and Windows Phone. If you have a mobile app idea that you’d like to discuss please Contact Us or learn more about our iPhone Android and Windows Phone app development services

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812