最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - Is NSCache Sendable? - Stack Overflow

programmeradmin6浏览0评论

Per the documentation:

You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

Does this mean that if you limited usage to just add, remove, and query you could consider NSCache sendable?

Per the documentation:

You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

Does this mean that if you limited usage to just add, remove, and query you could consider NSCache sendable?

Share Improve this question edited Mar 20 at 22:26 Alexander 63.5k13 gold badges105 silver badges168 bronze badges asked Mar 20 at 21:58 CalebKCalebK 7539 silver badges19 bronze badges 1
  • 2 No guarantees, but I would feel comfortable treating as sendable. That's as good of a guarantee as you could get, short of the API being updated with : Sendable. – Alexander Commented Mar 20 at 22:26
Add a comment  | 

1 Answer 1

Reset to default 1

An NSCache is safe to access from any thread, but the values themselves are only safe to access across isolation domains if they are Sendable (otherwise, you could accidentally use an NSCache to silently transfer values across isolation domains, which isn't valid to do). Ideally, this would be expressed as

extension NSCache: Sendable where KeyType: Sendable, ObjectType: Sendable {}

to help prevent accidentally passing around NSCache instances containing non-Sendable types.

Except, this isn't possible to express, because NSCache isn't truly generic in the Swift sense — KeyType and ObjectType don't really exist at runtime. The above extension yields

Type 'NSCache<KeyType, ObjectType>' cannot conditionally conform to protocol 'Sendable' because the type uses the Objective-C generics model

And since it's not safe to unconditionally mark NSCache: @unchecked Sendable, Sendable conformance was likely left off altogether.


While you can add your own @retroactive @unchecked Sendable conformance to NSCache (if you can guarantee you never place non-Sendable types in a cache), it's likely safer to wrap in a type you control yourself which is generic in the Swift sense, and can be correctly marked as Sendable:

import Foundation

struct SendableCache<Key: AnyObject, Value: AnyObject> {
    let cache: NSCache<Key, Value>
}

extension SendableCache: @unchecked Sendable where Key: Sendable, Value: Sendable {}
发布评论

评论列表(0)

  1. 暂无评论