0

I have a list in which objects gets added, modified and removed very frequently with asynchronous calls (Dart).

I don't want to update object by index to ensure that the target object is only modified.

I tried this

class Foo(){
    prop1;
    prop2;
}

class Bar(){
    List<Foo> list = List();

    Bar(){
        this.list = [foo1, foo2, foo3,];
    }

    updateList(someObj) async {
        this.list.asMap().entries.forEach(entry){
            if (entry.value.prop1 == someObj.prop1){
                this.list[entry.key].prop2 = someObj.prop2  
                // update object prop by index is working as of now, but 
               // multiple update calls can lead to update of wrong object 
            }
        }
    }
    
    addObj(someObj) async {
       ...
    }
    
    deleteObj(someObj) async {
       ...
    }
}

// calling function (async or without async) 
doSomething(){ 
   barObj.updateList(someObj); // this is a async function
   otherFunc(); // this line is not blocked
}

Can any one suggest me better way to update object in a list ensuring same object is updated?

  • Why are your methods marked as `async`? If you are just updating a `List` you can just do that? Also, I am not sure what you are worried about when updating based on index? You know your Dart code are running on a single thread unless you are using isolates which does not allow data sharing but only message passing? Can you try make a full working example which shows your problem? – julemand101 Nov 20 '20 at 10:48
  • Thanks for the reply. I am using async to not to block other calls in the function from where it is called. – Venkata Sri Krishna Chaitanya Nov 20 '20 at 11:12
  • You are aware that when you are marking a method as `async` it will be executed like a normal method until you are `awaiting` something? "An async function runs synchronously until the first await keyword. This means that within an async function body, all synchronous code before the first await keyword executes immediately.": https://dart.dev/codelabs/async-await#execution-flow-with-async-and-await – julemand101 Nov 20 '20 at 11:54
  • I am using async for non-blocking here. Can you please refer this [asynchronous vs non-blocking](https://stackoverflow.com/questions/2625493/asynchronous-vs-non-blocking) – Venkata Sri Krishna Chaitanya Nov 20 '20 at 13:08
  • Unless your lists are huge, it does not make much sense to try make this operations async (again, Dart are executing on one thread so you are not going to get more performance out of this but you can allow other code on the event queue to be executed). It will also make you code more complicated to use since your methods now returns a Future which it needs to await on if it wants to make sure your lists are updated. And, for getting this async behavior you need to spawn a future you can await on. – julemand101 Nov 20 '20 at 13:30
  • I think you need to rethink what you are doing. What issue do you have since you are thinking the solution would be to make these methods async? – julemand101 Nov 20 '20 at 13:31

0 Answers0