/// <summary>
 /// Delegate passed into the custom body writer to form the outgoing response.
 /// </summary>
 /// <param name="writer"></param>
 /// <param name="syncWriter"></param>
 private static void WriteResponse(XmlDictionaryWriter writer, SyncWriter syncWriter)
 {
     try
     {
         syncWriter.WriteFeed(writer);
     }
     catch (Exception exception)
     {
         // An exception at this point seems to be unrecoverable but ideally we should not hit exceptions since we are only
         // writing to the XmlDictionaryWriter. 
         SyncServiceTracer.TraceError("Exception in WriteResponse method. Details: {0}", WebUtil.GetExceptionMessage(exception));
     }
 }
		NSData CreateRequestBody ()
		{
			_syncWriter = (SyncWriter)new ODataAtomWriter (base.BaseUri);

			_syncWriter.StartFeed (_wrapper.CacheRequest.IsLastBatch, _wrapper.CacheRequest.KnowledgeBlob ?? new byte[0]);

			MemoryStream requestStream = new MemoryStream ();

			if (_wrapper.CacheRequest.RequestType == CacheRequestType.UploadChanges) {
				foreach (IOfflineEntity entity in _wrapper.CacheRequest.Changes) {
					// Skip tombstones that dont have a ID element.
					if (entity.ServiceMetadata.IsTombstone && string.IsNullOrEmpty (entity.ServiceMetadata.Id)) {
						continue;
					}

					string tempId = null;

					// Check to see if this is an insert. i.e ServiceMetadata.Id is null or empty
					if (string.IsNullOrEmpty (entity.ServiceMetadata.Id)) {
						if (_wrapper.TempIdToEntityMapping == null) {
							_wrapper.TempIdToEntityMapping = new Dictionary<string, IOfflineEntity> ();
						}

						tempId = Guid.NewGuid ().ToString ();

						_wrapper.TempIdToEntityMapping.Add (tempId, entity);
					}

					_syncWriter.AddItem (entity, tempId);
				}
			}

			if (base.SerializationFormat == SerializationFormat.ODataAtom) {
				_syncWriter.WriteFeed (XmlWriter.Create (requestStream));
			} else {
				_syncWriter.WriteFeed (JsonReaderWriterFactory.CreateJsonWriter (requestStream));
			}

			string result = "";

			requestStream.Seek (0, SeekOrigin.Begin);

			using (StreamReader reader = new StreamReader (requestStream)) {
				result = reader.ReadToEnd ();
			}

			requestStream.Flush ();
			requestStream.Close ();

			NSString dataString = new NSString (result);

			return dataString.DataUsingEncoding (NSStringEncoding.UTF8);
		}