Section 9/221 menit

9. Optimasi Dependencies (CocoaPods & SPM)

9. Optimasi Dependencies (CocoaPods & SPM)

CocoaPods: Pre-built Pods

Alih-alih mengkompilasi semua pod dari source setiap clean build, gunakan pre-built framework:

swift
# Podfile
platform :ios, '16.0'
use_frameworks! :linkage => :static

target 'MyApp' do
  pod 'Alamofire', '~> 5.8'
  pod 'Kingfisher', '~> 7.0'
  pod 'Lottie', '~> 4.3'

  # Cocoapods Binary Cache — gunakan binary pod yang sudah di-build
  # gem install cocoapods-binary-cache
  pod 'Firebase/Analytics', :binary => true
  pod 'Firebase/Crashlytics', :binary => true
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    target.build_configurations.each do |config|
      # Set minimum deployment target konsisten
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '16.0'
      # Matikan dSYM untuk pods di Debug
      if config.name == 'Debug'
        config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf'
      end
    end
  end
end

cocoapods-binary-cache

swift
# Install plugin
gem install cocoapods-binary-cache

# Konfigurasi cache di Podfile (tambahkan di atas)
plugin 'cocoapods-binary-cache', {
  binary_cache_path: '~/.cocoapods/binary-cache',
  prebuild_all_pods: false,
  check_manifest_lock: true
}

SPM: Mengelola Recompile

SPM mengkompilasi dependencies ke .build/ directory. Selama Package.resolved tidak berubah, SPM tidak recompile dependencies — ini sudah built-in.

Tapi ada gotcha: jika .build/ directory dihapus (misalnya oleh xcodebuild clean), semua dependencies dikompilasi ulang.

swift
# ❌ HINDARI: clean menghapus .build/, next build lambat
xcodebuild clean build -scheme MyApp

# ✓ LEBIH BAIK: hanya clean derived data app, bukan SPM cache
rm -rf ~/Library/Developer/Xcode/DerivedData/MyApp-*
xcodebuild build -scheme MyApp

Pilih Dependencies dengan Bijak

swift
# Pertanyaan sebelum menambah dependency baru:
1. Apakah saya bisa implement sendiri dalam < 2 jam? (pertimbangkan tidak pakai dependency)
2. Berapa jumlah source file dependency ini? (< 50 file = aman, > 500 file = hati-hati)
3. Apakah ada versi binary/XCFramework yang bisa dipakai?
4. Apakah dependency ini aktif di-maintain?