/// <summary>
        /// Called when [fai dl with error].
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="error">
        /// The error.
        /// </param>
        public void OnFailWithError(SyncRequestServerOperation request, Exception error)
        {
            this.serverRequest = null;
            if (this.cancelled)
            {
                return;
            }

            this.Delegate?.SyncDataSetsDidFail(this, error);
        }
        /// <summary>
        /// Cancels this instance.
        /// </summary>
        public void Cancel()
        {
            this.cancelled = true;

            if (this.serverRequest != null)
            {
                this.serverRequest.Delegate = null;
                this.serverRequest.Cancel();
                this.serverRequest = null;
            }

            this.Delegate = null;
        }
        /// <summary>
        /// Stars the dt in session.
        /// </summary>
        /// <param name="remoteSession">
        /// The remote session.
        /// </param>
        public void StartInSession(RemoteSession remoteSession)
        {
            this.serverRequest = new SyncRequestServerOperation(this.DataSets, this)
            {
                AlwaysPerform    = this.AlwaysPerformOperation,
                ContinueRequest  = this.ContinueRequest,
                TrackingDelegate = this.TrackingDelegate
            };

            if (!string.IsNullOrEmpty(this.LinkRecordIdentification))
            {
                this.serverRequest.RecordIdentification = this.LinkRecordIdentification;
            }

            this.Session?.ExecuteRequestInSession(this.serverRequest, remoteSession);
        }
        /// <summary>
        /// Called when [finish with response].
        /// </summary>
        /// <param name="request">
        /// The request.
        /// </param>
        /// <param name="json">
        /// The json.
        /// </param>
        public void OnFinishWithObjectResponse(SyncRequestServerOperation request, DataModelSyncDeserializer json)
        {
            this.serverRequest = null;
            if (this.cancelled)
            {
                return;
            }

            var sync        = new UPSynchronization(this.DataStore, this.ConfigStore);
            var ret         = sync.SyncWithDataObject(json);
            var recordCount = 0;
            var changedRecordIdentifications = new List <string>();
            var recordSyncArray1             = json.records;

            if (recordSyncArray1 != null)
            {
                foreach (var recordSyncObject in recordSyncArray1)
                {
                    var recordSync = recordSyncObject;

                    var dataSetName = recordSyncObject.datasetName;
                    if (string.IsNullOrEmpty(dataSetName))
                    {
                        continue;
                    }

                    var dataSet = this.ConfigStore.DataSetByName(dataSetName);
                    if (dataSet == null)
                    {
                        continue;
                    }

                    var rowArray = recordSync.rows;
                    if (rowArray == null)
                    {
                        continue;
                    }

                    recordCount += rowArray.Count;
                    foreach (JArray row in rowArray)
                    {
                        var rowinfo = row.First() as JArray;
                        var recordIdentification = StringExtensions.InfoAreaIdRecordId((string)rowinfo[0], (string)rowinfo[1]);
                        changedRecordIdentifications.Add(recordIdentification);
                    }
                }
            }

            if (ret > 0)
            {
                var errortext = $"storing records failed with errorcode #{ret}";

                // NSError.ErrorWithDomainCodeUserInfo(errortext, ret, null);
                var error = new Exception(errortext);
                this.Delegate?.SyncDataSetsDidFail(this, error);
            }
            else
            {
                var recordSyncArray = json.records;
                if (recordSyncArray != null)
                {
                    foreach (var recordSyncObject in recordSyncArray)
                    {
                        //var recordSync = recordSyncObject?.ToObject<Dictionary<string, object>>();

                        var dataSetName = recordSyncObject.datasetName;
                        if (string.IsNullOrEmpty(dataSetName))
                        {
                            continue;
                        }

                        var dataSet = this.ConfigStore.DataSetByName(dataSetName);
                        if (string.IsNullOrEmpty(dataSet?.SyncDocumentFieldGroupName))
                        {
                            continue;
                        }

                        var documentManager = new DocumentManager();
                        var rowArray        = recordSyncObject.rows;
                        if (rowArray == null)
                        {
                            continue;
                        }

                        foreach (JArray row in rowArray)
                        {
                            var rowinfo = row[0];
                            var recordIdentification = StringExtensions.InfoAreaIdRecordId((string)rowinfo[0], (string)rowinfo[1]);
                            var syncDocument         = new SyncDocument(recordIdentification, dataSet.SyncDocumentFieldGroupName, documentManager);
                            if (this.DocumentsToSync == null)
                            {
                                this.DocumentsToSync = new List <SyncDocument> {
                                    syncDocument
                                };
                            }
                            else
                            {
                                this.DocumentsToSync.Add(syncDocument);
                            }
                        }
                    }
                }

                this.TrackingDelegate.ClientFinishedWithRecordCount(recordCount);
                this.Delegate?.SyncDataSetsDidFinishSyncWithObject(this, json, changedRecordIdentifications);
            }
        }