locked
Getting ItemMetadata using FindItemMetadataByIndexedField RRS feed

  • Question

  • Hi,

    I am trying to get ItemMetadata using FindItemMetadataByIndexedField and I am getting ObjectDisposedException. here is a siplified code

    ///////////////   InitializeMetadataStore //////////////////////////////////////////////

    fields.Add(

    new FieldSchema(TIMESTAMP_COLUMNNAME, typeof(System.UInt64)));

    fields.Add(

    new FieldSchema(EXTERNAL_ID, typeof(string), 500));

    indexedFields.Add(

    new IndexSchema(EXTERNAL_ID, false));

    _metadataStore =

    SqlMetadataStore.CreateStore(replicaMetadataFile);

    _metadata = _metadataStore.InitializeReplicaMetadata(_idFormats,

    new SyncId(Guid.NewGuid()), fields, indexedFields);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    While creating a corrosponding destination object, I do

    string itemDataId = CreateObject(context.ChangeData);
    item = _metadata.CreateItemMetadata(change.ItemId, change.CreationVersion);

    item.SetCustomField(EXTERNAL_ID, itemDataId);

    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    While trying to detect changes I am doing

     

    private void UpdateMetadataStoreWithLocalChanges()

    {

     

    SyncVersion newVersion = new SyncVersion(0, _metadata.GetNextTickCount());

    _metadata.DeleteDetector.MarkAllItemsUnreported();

     

    foreach (T itemData in this.itemDataList)

    {

     

    ItemMetadata item = null;

     

    DateTime lastModifiedDate = GetLastModifiedDate(itemData);

     

    ulong timeStamp = (ulong)lastModifiedDate.Ticks;

     

    string itemId = GetItemDataId(itemData);

     

     

    //Look up an item's metadata by its ID...

     

    SyncId syncId = new SyncId(itemId);

    item = _metadata.FindItemMetadataById(syncId);

     

    if(null == item)

    {

     

    // Now see if item exists with external id

     

    IEnumerable<ItemMetadata> itemMetadataList = _metadata.FindItemMetadataByIndexedField(EXTERNAL_ID, itemId);

     

    if (itemMetadataList != null && itemMetadataList.Count() > 0)

    item = itemMetadataList.First(); // here I am getting ObjectDisposedException

    }

     

    if (null == item)

    {

     

    //New item, must have been created since that last time the metadata was updated.

     

    //Create the item metadata required for sync (giving it a SyncID and a version, defined to be a DWORD and a ULONGLONG

     

    //For creates, simply provide the relative replica ID (0) and the tick count for the provider (ever increasing)

    item = _metadata.CreateItemMetadata(syncId, newVersion);

    item.ChangeVersion = newVersion;

    SaveItemMetadata(item, timeStamp);

    }

     

    else

    {

     

    if (timeStamp > item.GetUInt64Field(TIMESTAMP_COLUMNNAME)) // the item has changed since the last sync operation.

    {

     

    //Changed Item, this item has changed since the last time the metadata was updated.

     

    //Assign a new version by simply stating "who" modified this item (0 = local/me) and "when" (tick count for the store)

    item.ChangeVersion = newVersion;

    SaveItemMetadata(item, timeStamp);

    }

     

    else

    {

     

    //Unchanged item, nothing has changes so just mark it as live so that the metadata knows it has not been deleted.

    _metadata.DeleteDetector.ReportLiveItemById(syncId);

    }

    }

    }

     

    //Now go back through the items that are no longer in the store and mark them as deleted in the metadata.

     

    //This sets the item as a tombstone.

     

    foreach (ItemMetadata item in _metadata.DeleteDetector.FindUnreportedItems())

    {

    item.ChangeVersion = newVersion;

    item.MarkAsDeleted(newVersion);

    SaveItemMetadata(item, 0);

    // set timestamp to 0 for tombstones

    }

    }

     


     

    • Moved by Max Wang_1983 Thursday, April 21, 2011 12:34 AM forum consolidation (From:SyncFx - Technical Discussion [ReadOnly])
    Monday, September 28, 2009 5:30 PM

Answers

  • To be more clear:

    if (itemMetadataList != null && itemMetadataList.GetEnumerator().MoveNext())

    item = itemMetadataList.GetEnumerator().Current;

    }

    That should work.  It's actually better than using Count() anyway because Count() will iterate through the whole list.

    Tuesday, September 29, 2009 11:50 PM

All replies

  • Hi,

    The issue appears to be that the underlying IEnumerable<ItemMetadata> returned to you does not support calling GetEnumerator() twice and getting 2 different IEnumerators.  So when you call Count() it gets the Enumerator and then disposes it and then you basically can't use it again.  Essentially this means you can't really use the extension methods.

    However, in this particular situation it's very easy to just use MoveNext() (and faster).

    -Jesse
    Tuesday, September 29, 2009 11:36 PM
  • To be more clear:

    if (itemMetadataList != null && itemMetadataList.GetEnumerator().MoveNext())

    item = itemMetadataList.GetEnumerator().Current;

    }

    That should work.  It's actually better than using Count() anyway because Count() will iterate through the whole list.

    Tuesday, September 29, 2009 11:50 PM