SimpleSyncProvider.UpdateItem 方法

命名空间: Microsoft.Synchronization.SimpleProviders
程序集: Microsoft.Synchronization.SimpleProviders(在 microsoft.synchronization.simpleproviders.dll 中)

语法

声明
Public MustOverride Sub UpdateItem ( _
    itemData As Object, _
    changeUnitsToUpdate As IEnumerable(Of SyncId), _
    keyAndExpectedVersion As ItemFieldDictionary, _
    recoverableErrorReportingContext As RecoverableErrorReportingContext, _
    <OutAttribute> ByRef keyAndUpdatedVersion As ItemFieldDictionary, _
    <OutAttribute> ByRef commitKnowledgeAfterThisItem As Boolean _
)
用法
Dim instance As SimpleSyncProvider
Dim itemData As Object
Dim changeUnitsToUpdate As IEnumerable(Of SyncId)
Dim keyAndExpectedVersion As ItemFieldDictionary
Dim recoverableErrorReportingContext As RecoverableErrorReportingContext
Dim keyAndUpdatedVersion As ItemFieldDictionary
Dim commitKnowledgeAfterThisItem As Boolean

instance.UpdateItem(itemData, changeUnitsToUpdate, keyAndExpectedVersion, recoverableErrorReportingContext, keyAndUpdatedVersion, commitKnowledgeAfterThisItem)
public abstract void UpdateItem (
    Object itemData,
    IEnumerable<SyncId> changeUnitsToUpdate,
    ItemFieldDictionary keyAndExpectedVersion,
    RecoverableErrorReportingContext recoverableErrorReportingContext,
    out ItemFieldDictionary keyAndUpdatedVersion,
    out bool commitKnowledgeAfterThisItem
)
public:
virtual void UpdateItem (
    Object^ itemData, 
    IEnumerable<SyncId^>^ changeUnitsToUpdate, 
    ItemFieldDictionary^ keyAndExpectedVersion, 
    RecoverableErrorReportingContext^ recoverableErrorReportingContext, 
    [OutAttribute] ItemFieldDictionary^% keyAndUpdatedVersion, 
    [OutAttribute] bool% commitKnowledgeAfterThisItem
) abstract
public abstract void UpdateItem (
    Object itemData, 
    IEnumerable<SyncId> changeUnitsToUpdate, 
    ItemFieldDictionary keyAndExpectedVersion, 
    RecoverableErrorReportingContext recoverableErrorReportingContext, 
    /** @attribute OutAttribute() */ /** @ref */ ItemFieldDictionary keyAndUpdatedVersion, 
    /** @attribute OutAttribute() */ /** @ref */ boolean commitKnowledgeAfterThisItem
)
JScript does not support passing value-type arguments by reference.

参数

  • itemData
    特定于提供程序的格式的项数据。
  • changeUnitsToUpdate
    一个包含要为某一项更新的变更单位的 SyncId 对象。如果未指定任何变更单位,该参数应为 Null(非空)。
  • keyAndExpectedVersion
    要更新的项的键和预期版本属性。提供程序必须执行开放式并发检查,以便确认目标上的项的版本是否与 keyAndExpectedVersion 中找到的值相对应。如果此检查失败,提供程序应通过使用 RecoverableErrorReportingContext 对象报告一个可恢复错误。
  • recoverableErrorReportingContext
    一个 RecoverableErrorReportingContext 对象,该对象用于报告在尝试更新某一项期间发生的可恢复错误。
  • keyAndUpdatedVersion
    返回更新项的键和更新的版本属性。如果返回值无效,则 Sync Framework 运行时将引发 ArgumentOutOfRangeException,这将结束该会话。
  • commitKnowledgeAfterThisItem
    返回在完成了对指定项的处理后 Sync Framework 运行时是否应将知识提交到元数据存储区。

备注

在 Sync Framework 检测到并加载了来自源的变更后,它必须将这些变更和相应的元数据变更应用于目标副本。位于目标的元数据变更由 Sync Framework 处理,但应用数据变更是特定于存储区的,应通过实现以下方法来处理:DeleteItemInsertItemUpdateItem

示例

下面的代码示例演示此方法的实现,该方法将更新应用于内存中的示例数据存储区。ItemTransfer 是一种简单的传输机制,当从源加载变更和将变更应用于目标时使用。若要在完整应用程序的上下文中查看此代码,请参见"Sync101 using Simple Sync Provider" 应用程序(可从 Sync Framework SDK 和 Code Gallery 获得)。

public override void UpdateItem(object itemData, 
    IEnumerable<SyncId> changeUnitsToUpdate, 
    ItemFieldDictionary keyAndExpectedVersion, 
    RecoverableErrorReportingContext recoverableErrorReportingContext, 
    out ItemFieldDictionary keyAndUpdatedVersion, 
    out bool commitKnowledgeAfterThisItem)
{
    ItemTransfer transfer = (ItemTransfer)itemData;
    ItemData dataCopy = new ItemData(transfer.ItemData);
    
    IDictionary<uint, ItemField> expectedFields = (IDictionary<uint, ItemField>)keyAndExpectedVersion;
    ulong idToUpdate = (ulong)expectedFields[CUSTOM_FIELD_ID].Value;

    if (_store.Contains(idToUpdate))
    {
        ulong timeStamp = _store.UpdateItem(idToUpdate, dataCopy);
        keyAndUpdatedVersion = _store.CreateItemFieldDictionary(transfer.Id);
    }
    else
    {
        // If the item to update does not exist, record an error on this change and 
        // continue with the rest of the session.
        recoverableErrorReportingContext.RecordRecoverableErrorForChange(new RecoverableErrorData(new Exception("Item not found in the store")));
        keyAndUpdatedVersion = null;
    }
    commitKnowledgeAfterThisItem = false;
}
Public Overrides Sub UpdateItem(ByVal itemData As Object, ByVal changeUnitsToUpdate As IEnumerable(Of SyncId), ByVal keyAndExpectedVersion As ItemFieldDictionary, ByVal recoverableErrorReportingContext As RecoverableErrorReportingContext, ByRef keyAndUpdatedVersion As ItemFieldDictionary, ByRef commitKnowledgeAfterThisItem As Boolean)
    Dim transfer As ItemTransfer = DirectCast(itemData, ItemTransfer)
    Dim dataCopy As New ItemData(transfer.ItemData)

    Dim expectedFields As IDictionary(Of UInteger, ItemField) = DirectCast(keyAndExpectedVersion, IDictionary(Of UInteger, ItemField))
    Dim idToUpdate As ULong = CULng(expectedFields(CUSTOM_FIELD_ID).Value)

    If _store.Contains(idToUpdate) Then
        Dim timeStamp As ULong = _store.UpdateItem(idToUpdate, dataCopy)
        keyAndUpdatedVersion = _store.CreateItemFieldDictionary(transfer.Id)
    Else
        ' If the item to update does not exist, record an error on this change and 
        ' continue with the rest of the session. 
        recoverableErrorReportingContext.RecordRecoverableErrorForChange(New RecoverableErrorData(New Exception("Item not found in the store")))
        keyAndUpdatedVersion = Nothing
    End If
    commitKnowledgeAfterThisItem = False
End Sub

请参阅

参考

SimpleSyncProvider 类
SimpleSyncProvider 成员
Microsoft.Synchronization.SimpleProviders 命名空间