Struct implementing PHPhotoLibraryChangeObserver - swift

I have a struct for my model and it needs to conform to a protocol that is NSObject only.
I am looking for a viable alternative to converting the model to a class. The requirements are:
Keeping the model a value type
Updating the model when photoLibraryDidChange is called
This would be the ideal implementation if PHPhotoLibraryChangeObserver would not require the implementation to be an NSObject
struct Model: PHPhotoLibraryChangeObserver {
var images:[UIImages] = []
fileprivate var allPhotos:PHFetchResult<PHAsset>?
mutating func photoLibraryDidChange(_ changeInstance: PHChange) {
let changeResults = changeInstance.changeDetails(for: allPhotos)
allPhotos = changeResults?.fetchResultAfterChanges
updateImages()
}
mutating func updateImages() {
// update self.images
...
}
}
I cannot pass the model to an external class implementing the observer protocol as then all the changes happen on the copy (its a value type...)
Any ideas? Best practices?
EDIT: Reformulated the question
EDIT 2: Progress
I have implemented a delegate as a reference type var of my model and pushed the data inside. Now photoLibraryDidChangeis not being called anymore.
This is the stripped down implementation:
class PhotoKitAdapter:NSObject, PHPhotoLibraryChangeObserver {
var allPhotos: PHFetchResult<PHAsset>?
var images:[UIImage] = []
override init(){
super.init()
}
func photoLibraryDidChange(_ changeInstance: PHChange) {
DispatchQueue.main.async {
if let changeResults = changeInstance.changeDetails(for: self.allPhotos!) {
self.allPhotos = changeResults.fetchResultAfterChanges
//this neve gets executed. It used to provide high quality images
self.updateImages()
}
}
}
func startFetching(){
let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)
PHPhotoLibrary.shared().register(self)
//this gets executed and fetches the thumbnails
self.updateImages()
}
fileprivate func appendImage(_ p: PHAsset) {
let pm = PHImageManager.default()
if p.mediaType == .image {
pm.requestImage(for: p, targetSize: CGSize(width: 1024, height: 768), contentMode: .default, options: nil){
image, _ in
if let im = image {
self.images.append(im)
}
}
}
}
fileprivate func updateImages() {
self.images = []
if let ap = allPhotos {
for index in 0..<min(ap.count, 10) {
let p = ap[index]
appendImage(p)
}
}
}
}
struct Model {
private var pkAdapter = PhotoKitAdapter()
var images:[UIImage] {
pkAdapter.images
}
func startFetching(){
pkAdapter.startFetching()
}
// example model implementation
mutating func select(_ image:UIImage){
// read directly from pkAdapter.images and change other local variables
}
}
I have put a breakpoint in photoLibraryDidChange and it just does not go there. I also checked that pkAdapter is always the same object and does not get reinitialised on "copy on change".
**EDIT: adding the model view **
This is the relevant part of the modelview responsible for the model management
class ModelView:ObservableObject {
#Published var model = Model()
init() {
self.model.startFetching()
}
var images:[UIImage] {
self.model.images
}
...
}
EDIT: solved the update problem
It was a bug in the simulator ... on a real device it works

I ended up with 2 possible designs:
decouple the PhotoKit interface completely from the model, let the modelview manage both and connect them, as the model view has access to the model instance.
create a PhotoKit interface as var of the model and push the mutable data that is generated by the PhotoKit interface inside it, so it can be changed with an escaping closure. The model is never called from the interface but just exposes the data inside the PhotoKit through a computer property.
I will show two sample implementation below. They are naive in many respect, they ignore performance problems by refreshing all pictures every time the PhotoLibrary is updated. Implementing proper delta updates (and other optimisation) would just clutter up the code and offer no extra insight on the solution to the original problem.
Decouple the PhotoKit interface
ModelView
class ModelView:ObservableObject {
var pkSubscription:AnyCancellable?
private var pkAdapter = PhotoKitAdapter()
#Published var model = Model()
init() {
pkSubscription = self.pkAdapter.objectWillChange.sink{ _ in
DispatchQueue.main.async {
self.model.reset()
for img in self.pkAdapter.images {
self.model.append(uiimage: img)
}
}
}
self.pkAdapter.startFetching()
}
}
Model
struct Model {
private(set) var images:[UIImage] = []
mutating func append(uiimage:UIImage){
images.append(uiimage)
}
mutating func reset(){
images = []
}
}
PhotoKit interface
class PhotoKitAdapter:NSObject, PHPhotoLibraryChangeObserver, ObservableObject {
var allPhotos: PHFetchResult<PHAsset>?
var images:[UIImage] = []
func photoLibraryDidChange(_ changeInstance: PHChange) {
DispatchQueue.main.async {
if let changeResults = changeInstance.changeDetails(for: self.allPhotos!) {
self.allPhotos = changeResults.fetchResultAfterChanges
self.updateImages()
self.objectWillChange.send()
}
}
}
func startFetching(){
PHPhotoLibrary.requestAuthorization{status in
if status == .authorized {
let allPhotosOptions = PHFetchOptions()
allPhotosOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
self.allPhotos = PHAsset.fetchAssets(with: allPhotosOptions)
PHPhotoLibrary.shared().register(self)
self.updateImages()
}
}
}
fileprivate func appendImage(_ p: PHAsset) {
// This actually appends multiple copies of the image because
// it gets called multiple times for the same asset.
// Proper tracking of the asset needs to be implemented
let pm = PHImageManager.default()
if p.mediaType == .image {
pm.requestImage(for: p, targetSize: CGSize(width: 1024, height: 768), contentMode: .default, options: nil){
image, _ in
if let im = image {
self.images.append(im)
self.objectWillChange.send()
}
}
}
}
fileprivate func updateImages() {
self.images = []
if let ap = allPhotos {
for index in 0..<min(ap.count, 10) {
let p = ap[index]
appendImage(p)
}
}
}
PhotoKit interface as property of the model
ModelView
class ModelView:ObservableObject {
#Published var model = Model()
}
Model
struct Model {
private var pkAdapter = PhotoKitAdapter()
var images:[UIImage] { pkAdapter.images }
}
PhotoKit interface
class PhotoKitAdapter:NSObject, PHPhotoLibraryChangeObserver{
var allPhotos: PHFetchResult<PHAsset>?
var images:[UIImage] = []
override init(){
super.init()
startFetching()
}
// the rest of the implementation is the same as before
...

Related

onDelete causing NSRangeException

In this app, there is a main screen (WorkoutScreen) that displays the contents of a list one at a time as it iterates through the list (current workout in a list of many). In a popOver, a list that contains all of the workouts appears and has the ability to add, delete or move items in that list.
When I delete the bottom most item, there is no error. When I delete any other item in the list I get this NSRangeException error that crashes the app:
/*
2022-04-24 15:41:21.874306-0400 Trellis
beta[9560:3067012] *** Terminating app due to
uncaught exception 'NSRangeException', reason:
'*** __boundsFail: index 3 beyond bounds [0 ..
2]'
*** First throw call stack:
(0x1809150fc 0x19914fd64 0x180a1e564 0x180a2588c
0x1808c0444 0x1852dcce4 0x1852e1400 0x185424670
0x185423df0 0x185428a40 0x18843e4a0 0x188510458
0x188fd83ec 0x10102f3bc 0x1010500a4 0x188494f4c
0x10102c664 0x10103e0d4 0x18841a944 0x10102be18
0x10103122c 0x18837b8ac 0x188363484 0x18834bb64
0x188371d20 0x1883b88e4 0x1b28fe910 0x1b28fe318
0x1b28fd160 0x18831e780 0x18832f3cc 0x1883f5e34
0x18834206c 0x188345f00 0x182eb0798 0x184613138
0x184605958 0x184619f80 0x184622874 0x1846050b0
0x183266cc0 0x1835015fc 0x183b7d5b0 0x183b7cba0
0x1809370d0 0x180947d90 0x180882098 0x1808878a4
0x18089b468 0x19c42638c 0x18323d088 0x182fbb958
0x1885547a4 0x188483928 0x1884650c0 0x10109a630
0x10109a700 0x1015b9aa4)
libc++abi: terminating with uncaught exception
of type NSException
dyld4 config:
DYLD_LIBRARY_PATH=/usr/lib/system/introspection
DYLD_INSERT_LIBRARIES=/Developer/usr/lib/libBacktrac
eRecording.dylib:/Developer/usr/lib/libMainThreadChecker.dylib:/Developer/Library/Private
Frameworks/DTDDISupport.framework/libViewDebuggerSupport.dylib
*** Terminating app due to uncaught exception 'NSRangeException', reason: '***
__boundsFail: index 3 beyond bounds [0 .. 2]'
terminating with uncaught exception of type NSException
(lldb)
*/
struct WorkoutScreen: View {
#EnvironmentObject var workoutList: CoreDataViewModel //calls it from environment
#StateObject var vm = CoreDataViewModel() //access on the page
#Environment(\.scenePhase) var scenePhase
var body: some View{
//displays the current item in the list
}
}
When I add an item to the list I get the error:
'''
CoreData: warning: Multiple NSEntityDescriptions claim the NSManagedObject subclass 'FruitEntity' so +entity is unable to disambiguate."
'''
Moving Items without adding or deleting any prior gives me this error upon closing the pop over:
'''
Error saving: Error Domain=NSCocoaErrorDomain Code=133020 "Could not merge changes." UserInfo={conflictList=(
"NSMergeConflict (0x2804f1480) for NSManagedObject (0x28327d900) with objectID '0x9ede5774e26501a4...
'''
Here is the core data and related functions:
class CoreDataViewModel: NSObject, ObservableObject {
private let container: NSPersistentContainer
private let context: NSManagedObjectContext
// Whenever you put your Core Data fetch in a view model, you should use an NSFetchedResultsController.
// This allows you to automatically update your #Published var when your Core Data store changes.
// You must inherit from NSObject to use it.
private let fetchResultsController: NSFetchedResultsController<FruitEntity>
#Published var savedEntities: [FruitEntity] = []
override init() {
container = NSPersistentContainer(name: "FruitsContainer")
container.loadPersistentStores { (description, error) in
if let error = error {
print("ERROR LOADING CORE DATA: \(error)")
}
else {
print("Successfully loaded core data")
}
}
context = container.viewContext
let request = NSFetchRequest<FruitEntity>(entityName: "FruitEntity")
let sort = NSSortDescriptor(keyPath: \FruitEntity.order, ascending: true)
request.sortDescriptors = [sort]
// This initializes the fetchResultsController
fetchResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
// Because you inherit from NSObject, you must call super.init() to properly init the parent class. The order of when
// this is to be called has changed.
super.init()
// Because this is a delegate action, you must set the delegate. Since the view model will respond, we set the delegate to self.
fetchResultsController.delegate = self
fetchFruits()
}
func fetchFruits() {
do {
// Instead of calling container.viewContext.fetch(request) which is static, use fetchResultsController.performFetch()
try fetchResultsController.performFetch()
// Make sure the fetch result is not nil
guard let fruitRequest = fetchResultsController.fetchedObjects else { return }
savedEntities = fruitRequest
// You do not need to let error. error is automatically captured in a do catch.
} catch {
print("Error fetching \(error)")
}
}
func addFruit(text: String, nummSets: Int16, nummWeights: Int16, nummReps: Int16, secOrRepz: Bool, orderNumz: Int64, multilimbz: Bool, countDownz: Int16, repTimez: Int16, restTimez: Int16, circuitz: Bool) {
let newFruit = FruitEntity(context: container.viewContext)
newFruit.name = text
newFruit.numOFSets = nummSets
newFruit.numOFWeight = nummWeights
newFruit.numOFReps = nummReps
newFruit.measure = secOrRepz
newFruit.order = orderNumz
newFruit.multiLimb = multilimbz
newFruit.countDownSec = countDownz
newFruit.timePerRep = repTimez
newFruit.restTime = restTimez
newFruit.circuit = circuitz
saveData()
}
func deleteFunction(indexSet: IndexSet) {
guard let index = indexSet.first else { return }
let entity = savedEntities[index]
container.viewContext.delete(entity)
saveData()
}
func saveData() {
do {
try context.save()
fetchFruits()
} catch let error {
print("Error saving: \(error)")
}
}
}
// This is your delegate extension that handles the updating when your Core Data Store changes.
extension CoreDataViewModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller:
NSFetchedResultsController<NSFetchRequestResult>) {
// Essentially, you are redoing just the fetch as the NSFetchedResultsController knows how to fetch from above
guard let fruits = controller.fetchedObjects as? [FruitEntity] else { return }
self.savedEntities = fruits
}
}
Here is the list struct:
struct WorkoutListPopUp: View {
#ObservedObject var vm = CoreDataViewModel()
#EnvironmentObject var listViewModel: ListViewModel
#EnvironmentObject var workoutList: CoreDataViewModel
//Too many #State var to list here
var body: some View {
Button (action: {
//this triggers the bug>
vm.addFruit(text: "Workout name", nummSets: Int16(addSets) ?? 3, nummWeights: Int16(addWeights) ?? 0, nummReps: Int16(addReps) ?? 8, secOrRepz: addSecOrReps, orderNumz: Int64((vm.savedEntities.count)), multilimbz: dualLimbs, countDownz: 10, repTimez: 3, restTimez: 60, circuitz: false)
loadNums()
}, label: {
Image(systemName: "plus")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width:20, height: 20)
.foregroundColor(Color.pink.opacity(1.0))
.padding(.top, 0)
})
List(){
ForEach(vm.savedEntities) {entity in
VStack{
EditWorkouts(entity: entity, prescribeMeasure: $prescribeMeasure, addReps: $addReps, measurePrescribed: $measurePrescribed, repTimePicker: $repTimePicker, repz: $repz, restPicker: $restPicker, setz: $setz, ready2Press: $ready2Press, workoutz: $workoutz, weightz: $weightz, setsRemaining: $setsRemaining, workoutNum: $workoutNum, workoutInstructions: $workoutInstructions, multiplelimbs: $multiplelimbs, showAllInfo: $showAllInfo)
//are these onChanges needed if "EditWorkouts" file is saving?
.onChange(of: entity.name) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.numOFSets) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.numOFReps) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.numOFWeight) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.measure) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.order) { text in
vm.saveData()
loadNums()
}
.onChange(of: entity.circuit) { text in
vm.saveData()
loadNums()
}
}
}
.onDelete(perform: vm.deleteFunction)
.onMove(perform: moveItem)
}
}
func loadNums(){
if vm.savedEntities.count > 0 {
workoutz = vm.savedEntities[workoutNum].name ?? "NO Name"
setz = String(vm.savedEntities[workoutNum].numOFSets)
weightz = String(vm.savedEntities[workoutNum].numOFWeight)
repz = String(vm.savedEntities[workoutNum].numOFReps)
multiplelimbs = vm.savedEntities[workoutNum].multiLimb
prescribeMeasure = vm.savedEntities[workoutNum].measure
if setsRemaining == 0 && ((workoutNum + 1) - (Int(vm.savedEntities.count)) == 0) {
workoutInstructions = "Goal: \(repz) \(measurePrescribed)"
}
else {
workoutInstructions = "Goal: \(repz) \(measurePrescribed)"
}
}
else {
workoutz = "Add a Workout 👉"
workoutInstructions = " "
}
}
func moveItem(indexSet: IndexSet, destination: Int) {
let source = indexSet.first!
if destination > source {
var startIndex = source + 1
let endIndex = destination - 1
var startOrder = vm.savedEntities[source].order
while startIndex <= endIndex {
vm.savedEntities[startIndex].order = startOrder
startOrder = startOrder + 1
startIndex = startIndex + 1
}
vm.savedEntities[source].order = startOrder
}
else if destination < source {
var startIndex = destination
let endIndex = source - 1
var startOrder = vm.savedEntities[destination].order + 1
let newOrder = vm.savedEntities[destination].order
while startIndex <= endIndex {
vm.savedEntities[startIndex].order = startOrder
startOrder = startOrder + 1
startIndex = startIndex + 1
}
vm.savedEntities[source].order = newOrder
}
vm.savedEntities[source].circuit = false
vm.saveData()
loadNums()
}
}
This is the EditWorkouts file that the WorkoutPopUp file connects to:
struct EditWorkouts: View {
#EnvironmentObject var workoutList: CoreDataViewModel
#StateObject var vm = CoreDataViewModel()
#EnvironmentObject var listViewModel: ListViewModel
let entity: FruitEntity
//too many #State vars to post
var body: some View {
VStack{
HStack{
//many lines of code for options that alter the respective workout on the list. All are followed by their version of:
//.onChange(of:
//vm.savedEntities[Int(entity.order)].multiLimb) { _ in
//vm.saveData()
//loadNums()"
//}
//-or-
//.onChange(of:vm.savedEntities[Int(entity.order)].circuit) { _ in
//entity.circuit = entity.circuit
//vm.saveData()
//}
}
}
}
}
Picture of CoreData FruitEntity:
Image
Thank you again for your time!!
There are a couple of issues with your code. I suspect one is the sole contributor to the crash, but the other may be contributing as well. First, the most likely culprit. If you use .onDelete(), you can't use id: \.self. The reason is pretty simple: the ForEach can get pretty confused as to which entity is which. .self is often not unique, and it really needs to be if you are deleting and rearranging things in the ForEach(), i.e. .onDelete() and .onMove().
The solution is simple. Whatever you are using in the ForEach should conform to Identifiable. Core Data managed objects all conform to Identifiable, so the fix is easy; remove the `id: .self``:
struct ListView: View {
#StateObject var vm = CoreDataViewModel()
var body: some View {
List {
ForEach(vm.savedEntities) {entity in
Text(entity.name ?? "")
}
.onDelete(perform: vm.deleteFunction)
}
// This just adds a button to create entities.
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
vm.addFruit()
} label: {
Image(systemName: "plus")
}
}
}
}
}
That fix alone will most likely stop the crash. However, I also noticed that you were having issues with your updates in your view. That is because you did not implement an NSFetchedResultsController and NSFetchedResultsControllerDelegate which updates your array when your Core Data store changes. Your view model should look like this:
import SwiftUI
import CoreData
class CoreDataViewModel: NSObject, ObservableObject {
private let container: NSPersistentContainer
private let context: NSManagedObjectContext
// Whenever you put your Core Data fetch in a view model, you should use an NSFetchedResultsController.
// This allows you to automatically update your #Published var when your Core Data store changes.
// You must inherit from NSObject to use it.
private let fetchResultsController: NSFetchedResultsController<FruitEntity>
#Published var savedEntities: [FruitEntity] = []
override init() {
container = NSPersistentContainer(name: "FruitsContainer")
container.loadPersistentStores { (description, error) in
if let error = error {
print("ERROR LOADING CORE DATA: \(error)")
}
else {
print("Successfully loaded core data")
}
}
context = container.viewContext
let request = NSFetchRequest<FruitEntity>(entityName: "FruitEntity")
let sort = NSSortDescriptor(keyPath: \FruitEntity.order, ascending: true)
request.sortDescriptors = [sort]
// This initializes the fetchResultsController
fetchResultsController = NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
// Because you inherit from NSObject, you must call super.init() to properly init the parent class. The order of when
// this is to be called has changed.
super.init()
// Because this is a delegate action, you must set the delegate. Since the view model will respond, we set the delegate to self.
fetchResultsController.delegate = self
// Renamed function to conform to naming conventions. You should use an active verb like fetch to start the name.
fetchFruits()
}
func fetchFruits() {
do {
// Instead of calling container.viewContext.fetch(request) which is static, use fetchResultsController.performFetch()
try fetchResultsController.performFetch()
// Make sure the fetch result is not nil
guard let fruitRequest = fetchResultsController.fetchedObjects else { return }
savedEntities = fruitRequest
// You do not need to let error. error is automatically captured in a do catch.
} catch {
print("Error fetching \(error)")
}
}
// This is just to be able to add some data to test.
func addFruit() {
var dateFormatter: DateFormatter {
let df = DateFormatter()
df.dateStyle = .short
return df
}
let fruit = FruitEntity(context: context)
fruit.name = dateFormatter.string(from: Date())
fruit.measure = false
fruit.numOfReps = 0
fruit.numOfSets = 0
fruit.numOfWeight = 0
fruit.order = 0
saveData()
}
func deleteFunction(indexSet: IndexSet) {
guard let index = indexSet.first else { return }
let entity = savedEntities[index]
container.viewContext.delete(entity)
saveData()
}
func saveData() {
do {
try context.save()
} catch let error {
print("Error saving: \(error)")
}
}
}
// This is your delegate extension that handles the updating when your Core Data Store changes.
extension CoreDataViewModel: NSFetchedResultsControllerDelegate {
func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
// Essentially, you are redoing just the fetch as the NSFetchedResultsController knows how to fetch from above
guard let fruits = controller.fetchedObjects as? [FruitEntity] else { return }
self.savedEntities = fruits
}
}
You will notice refreshID no longer exists in the view. It updates without it. Also, please note that by incorporating the data store init into your view model, you can't expand it to have other entities with other views. Each will have a different context and they will crash the app. You are better off having a controller class that creates a singleton for the Core Data store, such as what Apple gives you in the default set up.
In the end, I think you issue was a combination of using id: .self which is known to crash with .onDelete() AND the fact that you were using refreshID not NSFetchedResultsController to update the List.

Efficiently Fetching Multiple Images from Firebase

My app will have lists of images like shown in the below example:
To fetch an individual image from firebase, I defined a ViewModel class with the single image as a UIImage property. The appropriate view can then update with published changes to this ViewModel. With several--and in fact an arbitrary amount--of images in a list, I don't imagine that giving each one its own ViewModel will be efficient, especially because I imagine that I'll have to ForEach to loop through each image reference and create a ViewModel inside the parent view. This seems problematic because whenever the parent view is recreated I'm doing an O(n) operation to create and initialize view models for each image, not to mention the time it takes to re-fetch.
Is there a way to fetch multiple images from inside one ViewModel, storing them as [UIImage]? I would presumbably give this ViewModel an array of references. Alternatively, what architecture would you suggest to efficiently fetch any number of images from firebase given an array (of unknown size) of storage references?
I can provide more detail / code as needed.
This seems to work...
class MultipleImageFetcher: ObservableObject {
var firebaseManager: FirebaseManager
var imageDictionary: [String: UIImage] = [:]
#Published var isReady = false
init(_ firebaseManager: FirebaseManager) {
self.firebaseManager = firebaseManager
}
func fetchAllImages(references: [String]) {
for refID in references {
fetchIndividualImage(id: refID, totalNum:
references.count)
}
}
func fetchIndividualImage(id: String, totalNum: Int) {
let ref = getRefURL(uid: id)
ref.getData(maxSize: 2051240) { data, error in
if let error = error {
print("Error: \(error)")
return
}
self.imageDictionary[id] = UIImage(data: data!)
self.checkIsReady(num: totalNum)
}
}
func getRefURL(uid: String) -> StorageReference {
return firebaseManager.STORAGE.reference().child(uid)
}
func checkIsReady(num: Int) {
if imageDictionary.count == num {
self.isReady = true
} else {
self.isReady = false
}
}
}
struct ImageView: View {
#ObservedObject var fetcher: MultipleImageFetcher
init(_ firebaseManager: FirebaseManager) {
self.fetcher = MultipleImageFetcher(firebaseManager)
}
var body: some View {
Button {
fetcher.fetchAllImages(references:
["KXKFM94sk5OTyld2VsCQdhijd6m1", "MtgRaKMwyQfYX2uxuHs3iH3pLB52"])
} label: {
Text("Load Image")
}
if fetcher.isReady {
Text("Is Ready")
} else {
Text("Loading...")
}
}
}

Trying to set #published bool to true based on results from an API call

Hi first off I'm very new to swift and programing (coming from design field).
I'm trying to update doesNotificationsExist based on posts.count
I'm getting true inside the Api().getPosts {}
Where I print the following:
print("Api().getPosts")
print(doesNotificationExist)
but outside (in the loadData() {}) I still get false and not the #Publihed var doesNotificationExist:Bool = false doesn't update.
Please help me out, I would really appreciate some guidance to what I'm doing wrong and what I need to do.
Here is my code:
import SwiftUI
import Combine
public class DataStore: ObservableObject {
#Published var posts: [Post] = []
#Published var doesNotificationExist:Bool = false
init() {
loadData()
startApiWatch()
}
func loadData() {
Api().getPosts { [self] (posts) in
self.posts = posts
if posts.count >= 1 {
doesNotificationExist = true
}
else {
doesNotificationExist = false
}
print("Api().getPosts")
print(doesNotificationExist)
}
print("loadData")
print(doesNotificationExist)
}
func startApiWatch() {
Timer.scheduledTimer(withTimeInterval: 60, repeats: true) {_ in
self.loadData()
}
}
View where I'm trying to set an image based on store.doesNotificationsExist
StatusBarController:
import AppKit
import SwiftUI
class StatusBarController {
private var statusBar: NSStatusBar
private var statusItem: NSStatusItem
private var popover: NSPopover
#ObservedObject var store = DataStore()
init(_ popover: NSPopover)
{
self.popover = popover
statusBar = NSStatusBar.init()
statusItem = statusBar.statusItem(withLength: 28.0)
statusItem.button?.action = #selector(togglePopover(sender:))
statusItem.button?.target = self
if let statusBarButton = statusItem.button {
let itemImage = NSImage(named: store.doesNotificationExist ? "StatusItemImageNotification" : "StatusItemImage")
statusBarButton.image = itemImage
statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
statusBarButton.image?.isTemplate = true
statusBarButton.action = #selector(togglePopover(sender:))
statusBarButton.target = self
}
}
`Other none relevant code for the question`
}
It’s a closure and hopefully the #escaping one. #escaping is used to inform callers of a function that takes a closure that the closure might be stored or otherwise outlive the scope of the receiving function. So, your outside print statement will be called first with bool value false, and once timer is completed closure will be called changing your Bool value to true.
Check code below -:
import SwiftUI
public class Model: ObservableObject {
//#Published var posts: [Post] = []
#Published var doesNotificationExist:Bool = false
init() {
loadData()
// startApiWatch()
}
func loadData() {
getPost { [weak self] (posts) in
//self.posts = posts
if posts >= 1 {
self?.doesNotificationExist = true
}
else {
self?.doesNotificationExist = false
}
print("Api().getPosts")
print(self?.doesNotificationExist)
}
print("loadData")
print(doesNotificationExist)
}
func getPost(completion:#escaping (Int) -> ()){
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) {_ in
completion(5)
}
}
}
struct Test1:View {
#ObservedObject var test = Model()
var body: some View{
Text("\(test.doesNotificationExist.description)")
}
}

How to handle navigation with observables using Rx-MVVM-C

Hello I am trying to do a project with RxSwift and I am stuck trying to do in a properly way the connection between the Coordinator and the ViewModel.
Goal
Using observables, the Coordinator receives and event (in that case, when a row has been tapped) then does whatever.
Scenario
Giving a Post (String)
typealias Post = String
I have the following Coordinator:
class Coordinator {
func start() {
let selectedPostObservable = PublishSubject<Post>()
let viewController = ViewController()
let viewModel = ViewModel()
viewController.viewModel = viewModel
selectedPostObservable.subscribe { post in
//Do whatever
}
}
}
The selectedPostObservable is what I don't know how to connect it in a "clean" way with the viewModel.
As ViewModel:
class ViewModel {
struct Input {
let selectedIndexPath: Observable<IndexPath>
}
struct Output {
//UI Outputs
}
func transform(input: Input) -> Output {
let posts: [Post] = Observable.just(["1", "2", "3"])
//Connect with selectedindex
let result = input.selectedIndexPath
.withLatestFrom(posts) { $1[$0.row] }
.asDriver(onErrorJustReturn: nil)
return Output()
}
}
The result variable is what I should connect with selectedPostObservable.
And the ViewController (although I think is not relevant for the question):
class ViewController: UIViewController {
//...
var viewModel: ViewModel!
var tableView: UITableView!
//...
func bindViewModel() {
let input = ViewModel.Input(selectedIndexPath: tableView.rx.itemSelected.asObservable())
viewModel.transform(input: input)
}
}
Thank you so much.
Working with the structure you are starting with, I would put the PublishSubject in the ViewModel class instead of the Coordinator. Then something like this:
class ViewModel {
struct Input {
let selectedIndexPath: Observable<IndexPath>
}
struct Output {
//UI Outputs
}
let selectedPost = PublishSubject<Post>()
let bag = DisposeBag()
func transform(input: Input) -> Output {
let posts: [Post] = Observable.just(["1", "2", "3"])
//Connect with selectedindex
input.selectedIndexPath
.withLatestFrom(posts) { $1[$0.row] }
.bind(to: selectedPost)
.disposed(by: bag)
return Output()
}
}
class Coordinator {
func start() {
let viewController = ViewController()
let viewModel = ViewModel()
viewController.viewModel = viewModel
viewModel.selectedPost.subscribe { post in
//Do whatever
}
.disposed(by: viewModel.bag)
}
}

MVVM with realm: Passing Realm-results across threads?

Using Xcode-8.2.1, Swift-3.0.2, RealmSwift-2.2.0, iOS-Simulator-10:
I try applying the MVVM pattern (explained by Steve Scott here) using Realm.
Everything works until the moment (inside the VIEW-part - see below) where I try to access a viewmodel-property. It says: Realm accessed from incorrect thread
How could I still make the MVVM-pattern do its job of separating model, view-model and view but, on the same time, get thread-safety with realm ?
Is there a way to make Realm-results (i.e. Results<BalancesDataEntry>) being passed across threads ??
Here is my code:
(the issue happens at the very bottom, inside the View-part)
// REALM-OBJECT:
import Foundation
import RealmSwift
class BalancesDataEntry: Object {
dynamic var category: String = ""
dynamic var index: Int = 0
}
MODEL:
import Foundation
import RealmSwift
class MVVMCBalancesModel: BalancesModel
{
fileprivate var entries = [BalancesDataEntry]()
let realm = try! Realm()
init() {
self.createDataEntries()
}
fileprivate func createDataEntries() {
let myBalance = BalancesDataEntry()
myBalance.index = 0
myBalance.category = "Love"
try! self.realm.write {
self.realm.deleteAll()
self.realm.add(myBalance)
}
}
func getEntries(_ completionHandler: #escaping (_ entries: [BalancesDataEntry]) -> Void)
{
// Simulate Aysnchronous data access
DispatchQueue.global().async {
let realmThread = try! Realm()
let returnArray: [BalancesDataEntry] = Array(realmThread.objects(BalancesDataEntry.self))
completionHandler(returnArray)
}
}
}
VIEW-MODEL:
import Foundation
import RealmSwift
class MVVMCBalancesViewModel: BalancesViewModel
{
weak var viewDelegate: BalancesViewModelViewDelegate?
weak var coordinatorDelegate: BalancesViewModelCoordinatorDelegate?
fileprivate var entries: [BalancesDataEntry]? {
didSet {
viewDelegate?.entriesDidChange(viewModel: self)
}
}
var model: BalancesModel? {
didSet {
entries = nil;
model?.getEntries({ (myEntries) in
self.entries = myEntries
})
}
}
var title: String {
return "My Balances"
}
var numberOfEntries: Int {
if let entries = entries {
return entries.count
}
return 0
}
func entryAtIndex(_ index: Int) -> BalancesDataEntry?
{
if let entries = entries , entries.count > index {
return entries[index]
}
return nil
}
func useEntryAtIndex(_ index: Int)
{
if let entries = entries, let coordinatorDelegate = coordinatorDelegate , index < entries.count {
coordinatorDelegate.balancesViewModelDidSelectData(self, data: entries[index])
}
}
}
VIEW:
import UIKit
class MVVMCBalancesViewController: UIViewController {
#IBOutlet weak var label1Outlet: UILabel!
#IBOutlet weak var label2Outlet: UILabel!
var viewModel: BalancesViewModel? {
willSet {
viewModel?.viewDelegate = nil
}
didSet {
viewModel?.viewDelegate = self
refreshDisplay()
}
}
var isLoaded: Bool = false
func refreshDisplay()
{
if let viewModel = viewModel , isLoaded {
// !!!!!!! HERE IS THE ISSUE: Realm accessed from incorrect thread !!!!
self.label1Outlet.text = viewModel.entryAtIndex(0)?.category
self.label2Outlet.text = viewModel.entryAtIndex(1)?.category
} else {
}
}
override func viewDidLoad()
{
super.viewDidLoad()
isLoaded = true
refreshDisplay();
}
}
extension MVVMCBalancesViewController: BalancesViewModelViewDelegate
{
func entriesDidChange(viewModel: BalancesViewModel)
{
}
}
You can use ThreadSafeReference to pass Realm's thread-confined types (Object, Results, List, LinkingObjects) to a different thread. The documentation's section on Passing Instances Across Threads contains this example of passing a single instance of an Object subclass across threads:
let person = Person(name: "Jane")
try! realm.write {
realm.add(person)
}
let personRef = ThreadSafeReference(to: person)
DispatchQueue(label: "background").async {
let realm = try! Realm()
guard let person = realm.resolve(personRef) else {
return // person was deleted
}
try! realm.write {
person.name = "Jane Doe"
}
}
It can be used similarly for Results.
I have found a workaround (see below): Maybe you have better solutions - please let me know!
Here is my github-code realm_mvvm_c on github
After introducing a new protocol and making (pretty much everything) conform to it, things worked out.
Here is the protocol called DataEntry:
import Foundation
protocol DataEntry: class {
var idx: Int { get set }
var category: String { get set }
}
Now, make everything conform to it, such as
--> the realm object (i.e. class BalancesDataEntry: Object, DataEntry {...)
--> the getEntries return value (i.e. func getEntries(_ completionHandler: #escaping (_ entries: [DataEntry]) -> Void))
--> the View-Model's entries (i.e. fileprivate var entries: [DataEntry]? {..)
--> all the corresponding Model- and View-Model protocols also need the DataEntry datatype (see git-repo for complete picture)
After that, it was enough to change the completion-handler return-array of the MODEL's method getEntries(..) to a newly created object-instance (ie. DataEntryDub) that is keept conform to the DataEntry protocol:
func getEntries(_ completionHandler: #escaping (_ entries: [DataEntry]) -> Void)
{
// Simulate Aysnchronous data access
DispatchQueue.global().async {
let realmThread = try! Realm()
class DataEntryDub: DataEntry {
var idx: Int
var category: String
init(idx: Int, category: String) {
self.idx = idx
self.category = category
}
}
var returnArray = [DataEntry]()
for entry in realmThread.objects(BalancesDataEntry.self) {
returnArray.append(DataEntryDub(idx: entry.idx, category: entry.category))
}
completionHandler(returnArray)
}
}
Here is my github-code realm_mvvm_c on github