cordova-plugin-nativestorage: iOS Sporadic Crash on SetItem

I am using this plugin through Ionic2 in an enterprise app with thousands of users each day. I have been seeing this crash report in XCode for a long time now:

image

As you can see, this has been happening on setItem() method. Here is the code from the NativeStorage.m file:

(void) setItem: (CDVInvokedUrlCommand*) command
{
	[self.commandDelegate runInBackground:^{
		CDVPluginResult* pluginResult = nil;
		NSString* reference = [command.arguments objectAtIndex:0];
		NSString* aString = [command.arguments objectAtIndex:1];

		if(reference!=nil)
		{
			NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
			[defaults setObject: aString forKey:reference];
			BOOL success = [defaults synchronize];
			if(success) pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:aString];
			else pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsInt:1]; //Write has failed

		}
		else
		{
			pluginResult = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsInt:3]; //Reference was null
		}
		[self.commandDelegate sendPluginResult:pluginResult callbackId: command.callbackId];
	}];
}

And this is only happening every once in a while (not every time I use the setItem method). Because, as I said, I have thousands of users each day and this has, for example, only happened 73 times in the past two weeks.

image

The crash backtrace goes back to this line:

[defaults setObject: aString forKey:reference];

Could you please clarify what is this line doing?

About this issue

Most upvoted comments

There seems to be less occurrence of reporting this error at the moment.

@Maziar-Fotouhi interesting…lets explore this further with special focus on your insights.

cc @GillesC