-
Notifications
You must be signed in to change notification settings - Fork 121
[MBL-19677][S/P/T] Structured Concurrency foundations #3852
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
Draft
petkybenedek
wants to merge
14
commits into
master
Choose a base branch
from
chore/MBL-19677-Structured-Concurrency-foundation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f0c61ea
Create AsyncStore
petkybenedek a179ad8
Correct renaming
petkybenedek 1595afa
Created unit tests for AsyncStore
petkybenedek 666c444
Remove newline
petkybenedek 4abcbb4
Create AsyncFecthedResults and add getFirstEntity
petkybenedek 6d1c18a
Add equatable conformance
petkybenedek 2126f83
Refactor
petkybenedek 7050c72
Remove if expression
petkybenedek c2da741
Remove if expression
petkybenedek 035d971
Resolve build errors
petkybenedek 18a0a74
Correct indentation
petkybenedek b2a42b2
Requested changes
petkybenedek 5dbb3ae
Simplifying call chains
petkybenedek d481da6
Correct tests
petkybenedek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
Core/Core/Common/CommonModels/Store/AsyncFetchedResults.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| // | ||
| // This file is part of Canvas. | ||
| // Copyright (C) 2026-present Instructure, Inc. | ||
| // | ||
| // This program is free software: you can redistribute it and/or modify | ||
| // it under the terms of the GNU Affero General Public License as | ||
| // published by the Free Software Foundation, either version 3 of the | ||
| // License, or (at your option) any later version. | ||
| // | ||
| // This program is distributed in the hope that it will be useful, | ||
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| // GNU Affero General Public License for more details. | ||
| // | ||
| // You should have received a copy of the GNU Affero General Public License | ||
| // along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| // | ||
|
|
||
| import Foundation | ||
| import CoreData | ||
|
|
||
| @preconcurrency | ||
| public final class AsyncFetchedResults<ResultType: NSFetchRequestResult> { | ||
| private let request: NSFetchRequest<ResultType> | ||
| private let context: NSManagedObjectContext | ||
|
|
||
| public init( | ||
| request: NSFetchRequest<ResultType>, | ||
| context: NSManagedObjectContext | ||
| ) { | ||
| self.request = request | ||
| self.context = context | ||
| } | ||
|
|
||
| public func fetch() async throws -> [ResultType] { | ||
| try await context.perform { | ||
| try self.context.fetch(self.request) | ||
| } | ||
| } | ||
|
|
||
| public func stream() -> AsyncThrowingStream<[ResultType], Error> { | ||
| AsyncThrowingStream { continuation in | ||
| let observer = FetchedResultsObserver( | ||
| request: request, | ||
| context: context, | ||
| continuation: continuation | ||
| ) | ||
|
|
||
| continuation.onTermination = { _ in | ||
| observer.cancel() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private final class FetchedResultsObserver<ResultType: NSFetchRequestResult>: NSObject, NSFetchedResultsControllerDelegate { | ||
| private var controller: NSFetchedResultsController<ResultType>? | ||
| private let continuation: AsyncThrowingStream<[ResultType], Error>.Continuation | ||
| private let context: NSManagedObjectContext | ||
|
|
||
| init( | ||
| request: NSFetchRequest<ResultType>, | ||
| context: NSManagedObjectContext, | ||
| continuation: AsyncThrowingStream<[ResultType], Error>.Continuation | ||
| ) { | ||
| self.continuation = continuation | ||
| self.context = context | ||
| super.init() | ||
|
|
||
| context.perform { [weak self] in | ||
| guard let self else { return } | ||
|
|
||
| self.controller = NSFetchedResultsController( | ||
| fetchRequest: request, | ||
| managedObjectContext: context, | ||
| sectionNameKeyPath: nil, | ||
| cacheName: nil | ||
| ) | ||
| self.controller?.delegate = self | ||
|
|
||
| do { | ||
| try self.controller?.performFetch() | ||
| self.sendElement() | ||
| } catch { | ||
| continuation.finish(throwing: NSError.instructureError("Error while reading from Core Data")) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private func sendElement() { | ||
| context.perform { [weak self] in | ||
| guard let self else { return } | ||
| let entities = self.controller?.fetchedObjects ?? [] | ||
| self.continuation.yield(entities) | ||
| } | ||
| } | ||
|
|
||
| func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) { | ||
| sendElement() | ||
| } | ||
|
|
||
| func cancel() { | ||
| context.perform { [weak self] in | ||
| self?.controller?.delegate = nil | ||
| self?.controller = nil | ||
| self?.continuation.finish() | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking if we can get rid of this
@preconcurrencyattribute be used with our newly introduced Async types. How about using it with@preconcurrency import CoreDataas that was by Apple, and then move the fetch method to an extension ofNSManagedObjectContext.This way we keep the new code committed to the async/await model, isolating any source of pre-concurrency as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, thanks!