Section 10/221 menit

10. Caching: Local dan Remote Build Cache

10. Caching: Local dan Remote Build Cache

Local DerivedData Sharing

Xcode menyimpan hasil kompilasi di DerivedData. Antar developer yang menggunakan mesin berbeda, cache ini tidak dibagikan. Tapi ada solusi:

Xcode's Shared Build Cache (Xcode 16+):

swift
Xcode → Settings → Locations → Shared Cache
Aktifkan untuk menyimpan compiled modules di lokasi yang bisa dibagikan via network drive atau git LFS

Remote Build Cache dengan Bazel

Untuk team besar, Bazel dengan remote caching adalah solusi paling efektif:

swift
# BUILD file untuk Bazel (contoh sederhana)
# WORKSPACE dan BUILD files menggantikan Xcode project

ios_library(
    name = "NetworkKit",
    srcs = glob(["Sources/NetworkKit/**/*.swift"]),
    deps = [],
)

ios_library(
    name = "UIComponents",
    srcs = glob(["Sources/UIComponents/**/*.swift"]),
    deps = [":NetworkKit"],
)

ios_application(
    name = "MyApp",
    bundle_id = "com.example.myapp",
    families = ["iphone"],
    minimum_os_version = "16.0",
    deps = [":NetworkKit", ":UIComponents"],
)
swift
# .bazelrc — konfigurasi remote cache
build --remote_cache=grpc://cache.internal:9090
build --remote_upload_local_results=true

# Build dengan remote cache
bazel build //App:MyApp
# Jika cache hit: artifact diunduh, tidak dikompilasi ulang

Tuist + Hashable Cache

Jika menggunakan Tuist, manfaatkan tuist cache:

swift
# Generate dan cache binary frameworks
tuist cache warm --dependencies-only

# Build app menggunakan cached binaries
tuist generate  # Otomatis gunakan cached frameworks jika tersedia

# Cache key berbasis konten hash file source — sangat akurat
swift
// Project.swift — aktifkan cache
import ProjectDescription

let config = Config(
    cache: .cache(
        profiles: [.profile(configuration: "Debug")]
    )
)