hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.83k stars 115 forks source link

very heavy load on the CPU #157

Closed Meffistofel1992 closed 6 months ago

Meffistofel1992 commented 11 months ago

The project is not big Mainly uses @Injected(.) @InjectedObject(.)

when I'm just writing code or switching between xcode files, it lags a lot what can you suggest? I like the library, but it is difficult to work because of lags

xCode 15.0 Releas Mac m1 pro 2021

The more I use @injected or @InjectedObject the more often the CPU hangs and spikes

EvanCooper9 commented 11 months ago

Came here to report the same issue for my project. Simply including the line import Factory causes text editing to lag when typing, adding/deleting new lines, etc (even on small files).

Same specs: Xcode 15.0 (15A240d) MBP M1 Pro 2021

Anecdotally, this wasn't an issue with Xcode 14.x. I only started experiencing this when I upgraded to Sonoma & Xcode 15. Unfortunately I can't go back to Xcode 14.

hmlongco commented 11 months ago

How is Factory imported into the project? SPM?

EvanCooper9 commented 11 months ago

For me, yes I'm using SPM

edkhrian commented 11 months ago

I experienced the same problem and I couldn't even edit files. So I opened the project with another editor and added types to all injected factories (find/replace is your savior). For example, instead of

struct MyView: View {
  @Injected(\.apiRepository) var apiRepository
  // ...
}

I added type to the injected factory

struct MyView: View {
  @Injected(\.apiRepository) var apiRepository: APIRepository
  // ...
}

And it solved the problem. I guess xcode lags trying to resolve all implicitly injected types

hmlongco commented 11 months ago

Can't reproduce this, but it looks as if specifying the type can alleviate the issue. Did make one change to InjectedObject to explicitly specify type of internal StateObject.

VladOrackle commented 10 months ago

Same issue on Xcode 15.0.1, specifying types helps, but it's strange that it even happens

nashfive commented 10 months ago

I noticed exactly the same problem. Sometimes it's even worse, I have to force-quit Xcode (forever beach-balling...) and I even have to edit the source with an external text editor. Xcode 15.1b2 seems to fix the worst case scenario but some source files still trigger hangs when scrolling. Adding explicit types as suggested, fixes it.

KiritoBeater commented 10 months ago

I experienced the same problem, and my solution is to replace @Injected(\. keypath) with resolve(\.keypath).

For example, instead of

struct MyView: View {
  @Injected(\.apiRepository) var apiRepository
  // ...
}

Using the the global functions which Factory provide. https://hmlongco.github.io/Factory/documentation/factory/resolutions/#Global-Keypath-Resolution-from-Shared-Container

struct MyView: View {
  var apiRepository = resolve(\.apiRepository) 
  // ...
}

it solved the problem and avoid direct referencing of types.

nashfive commented 7 months ago

Xcode 15.3 seems to have fixed the problem... I don't have the issue anymore so far, so I hope it will stay like this 🎉