简体   繁体   English

Swift中的C#阻止集合

[英]C# blockingcollection in Swift

I am working on converting a C# application to Swift. 我正在将C#应用程序转换为Swift。 Everything is going fine, but I am stuck at the point where the dev in the C# program used a Blocking collection: 一切都很好,但是我陷入了C#程序中的开发人员使用Blocking集合的地步:

public static BlockingCollection<MouseUsageMessage> mouseUsageMessageQueue = new BlockingCollection<MouseUsageMessage>();

Later on, they're adding something to the queue, just a simple integer passed to a class which returns a message that is added to the queue: 稍后,他们将一些东西添加到队列中,只是将一个简单的整数传递给类,该类将返回添加到队列中的消息:

mouseUsageMessageQueue.Add(new MouseUsageMessage(0));

Then the program goes through the queue with a foreach using the ConsumingEnumerable of each message: 然后,程序使用每个消息的ConsumingEnumerable通过foreach遍历队列:

foreach(MouseUsageMessage msg in mouseUsageMessageQueue.GetConsumingEnumerable()){
     // do something
}

I don't have enough experience with Swift to know how I can do the same as described above in Swift. 我对Swift的经验不足,无法知道如何做与Swift中上述相同的操作。 So my question here is: How can I do the same as what is done in C# (see code above) in Swift? 所以我在这里的问题是:我该如何做与Swift中C#(请参见上面的代码)所做的相同?

I'm not super experienced with C#, but I understand that BlockingCollection is just a thread safe array. 我对C#并没有超级的经验,但是我知道BlockingCollection只是一个线程安全的数组。 In Swift arrays are not thread safe, however, you could wrap your arrays with a generic class and restrict access to it with a dispatch queue. 在Swift数组中,线程不是安全的,但是,您可以使用通用类包装数组,并通过调度队列限制对其的访问。 I've seen they call this a synchronized collection on the internet, but the examples I've seen don't make use of the Collection protocol, losing a lot of functionality. 我已经看到他们在Internet上将其称为同步集合,但是我看到的示例并未利用Collection协议,因此失去了很多功能。 Here is an example I wrote: 这是我写的一个示例:

public class BlockingCollection<T>: Collection {

    private var array: [T] = []
    private let queue = DispatchQueue(label: "com.myapp.threading") // some arbitrary label

    public var startIndex: Int {
        return queue.sync {
            return array.startIndex
        }
    }

    public var endIndex: Int {
        return queue.sync {
            return array.endIndex
        }
    }

    public func append(newElement: T) {
        return queue.sync {
            array.append(newElement)
        }
    }

    public subscript(index: Int) -> T {
        set {
            queue.sync {
                array[index] = newValue
            }
        }
        get {
            return queue.sync {
                return array[index]
            }
        }
    }

    public func index(after i: Int) -> Int {
        return queue.sync {
            return array.index(after: i)
        }
    }
}

Thanks to the powerful protocols extensions in Swift, you get all the typical functions of a collection (forEach, filter, map, etc) for free. 借助Swift中强大的协议扩展,您可以免费获得集合的所有典型功能(forEach,filter,map等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM