Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-1522] genericization of PHFetchResult makes it unusable with PHPhotoLibraryChangeObserver #44131

Open
mattneub opened this issue May 14, 2016 · 12 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@mattneub
Copy link

Previous ID SR-1522
Radar None
Original Reporter @mattneub
Type Bug
Environment

Toolchain: Swift Development Snapshot 2016-05-09 (a), Xcode 7.3.1, iOS 9

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee @jckarter
Priority Medium

md5: ce5be1fe7ca94bd4c8d4af27956a9463

Issue Description:

In Swift 3, PHFetchResult now arrives into Swift as a generic. Unfortunately, this makes it impossible to use. A typical use case is:

1. Call PHAssetCollection.fetchAssetCollections to obtain a PHFetchResult.

2. Store the PHFetchResult for later use.

3. In your PHPhotoLibraryChangeObserver's implementation of photoLibraryDidChange, receive a PHChange object.

4. Call that PHChange object's changeDetails(for:) to find out what changed.

Unfortunately, step 1 yields a PHFetchResult<PHAssetCollection>, whereas step 4 requires a PHFetchResult<AnyObject>. Generics are not covariant (except for specially treated generics like Array), so this conversion is impossible. Thus, the change to a generic has broken existing code.

@belkadan
Copy link
Contributor

cc @jckarter, @DougGregor

@jckarter
Copy link
Member

As a workaround, you can do result as! PHFetchResult<AnyObject> to force the type change. Dynamic-casting ObjC objects only checks the class, not the generic parameters (which don't exist at runtime).

@jckarter
Copy link
Member

We had a report that the as.

@belkadan
Copy link
Contributor

Another alternative is to jump up to AnyObject and then back down to the new type.

@mattneub
Copy link
Author

@belkadan I noticed that that seemed to quiet the compiler in a "baby" demo example, but in my real app it crashes compiler (Command failed due to signal: Illegal instruction: 4). @jckarter Same problem with unsafeBitCast.

@jckarter
Copy link
Member

Thanks for the heads up @mattneub. Let me see if I can whip up a fix for both as! and unsafeBitCast real quick.

@jckarter
Copy link
Member

@mattneub Are you able to attach your "real" app, or something like it that reproduces the crash? I just gave a naive example a whirl and wasn't able to crash it.

@jckarter
Copy link
Member

For reference, I just tried this, and couldn't get it to crash:

  func test(list: PHCollectionList, change: PHChange) {
    let fetched =  PHAssetCollection.fetchCollections(in: list, options: nil)
    
    change.changeDetails(for: fetched as! PHFetchResult<AnyObject>)
    
  }

@mattneub
Copy link
Author

@jckarter You're right, I'm wrong. The compiler crash was caused by a different bug: we can't subscript a PHFetchResult. I'll file that separately.

@belkadan
Copy link
Contributor

Looks like we have that internally: specifically, accessing a method or property that takes or returns a bridged type based on the generic parameter (like Array, Set, or Dictionary) causes the failure. (Thanks for helping look into this, @rjmccall.)

@Dante-Broggi
Copy link
Contributor

Is this resolved, or no longer valid? If either, this should be closed.

@mattneub
Copy link
Author

Looks invalid to me. The important one is https://bugs.swift.org/browse/SR-1576

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

4 participants