Cookie deletion while enumerating: safe?

60 Views Asked by At

Given this code:

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (NSHTTPCookie *cookie in cookieStorage.cookies) {
    if (/* my specific condition that is true multiple times */) {
        [cookieStorage deleteCookie:cookie];
    }
}

This does not throw an exception, implying that the mutation of the cookie jar during the enumeration is safe. What I want to know is, why? Is this always going to be safe, or does it just happen to be safe due to some implementation detail that may change?

1

There are 1 best solutions below

3
On BEST ANSWER

Practical answer: the clue is in the header; cookies is defined as:

@property (readonly, copy) NSArray *cookies;

Per the Apple documentation, that "Copy Properties Maintain Their Own Copies", i.e. the cookies array is not the live storage, it is a copy. What you receive from it is a snapshot.

When you call deleteCookie, the new list of cookies is created and subsequent calls to cookies will return a then-up-to-date copy but the one you still have hold of from previously isn't affected.

In pure code terms, copy implies setCookies: (if it exists; it isn't publicly confirmed or denied) will take a copy of whatever is passed in. So if what was passed in was mutable it'll become immutable. So whatever you receive will be immutable. So it definitely won't mutate.

From either piece of reasoning, you are not mutating, and are not able to mutate, the actual thing you're iterating. You're mutating the cookie store but iterating through a copy of one piece of its contents.