MGLOfflinePack

@interface MGLOfflinePack : NSObject

An MGLOfflinePack represents a collection of resources necessary for viewing a region offline to a local database.

To create an instance of MGLOfflinePack, use the +[MGLOfflineStorage addPackForRegion:withContext:completionHandler:] method. A pack created using -[MGLOfflinePack init] is immediately invalid.

Example

MGLOfflineStorage.shared.addPack(for: region, withContext: context) { (pack, error) in
guard let pack = pack else {
    // If adding the pack fails, log an error to console.
    print("Error:", error?.localizedDescription ?? "unknown error adding pack at \(#file)(\(#line)) in \(#function)")
    return
}

// Start an MGLOfflinePack download
pack.resume()
}

region

The region for which the pack manages resources.

Declaration

Objective-C

@property (nonatomic, readonly) id<MGLOfflineRegion> _Nonnull region;

Swift

var region: MGLOfflineRegion { get }

context

Arbitrary data stored alongside the downloaded resources.

The context typically holds application-specific information for identifying the pack, such as a user-selected name.

To change the value of this property, use the -setContext:completionHandler: method. If you access this property after calling that method but before its completion handler is called, this property’s value may not reflect the new value that you specify.

Declaration

Objective-C

@property (nonatomic, readonly) NSData *_Nonnull context;

Swift

var context: Data { get }

-setContext:completionHandler:

Associates arbitrary contextual data with the offline pack, replacing any context that was previously associated with the offline pack.

Setting the context is asynchronous. The context property may not be updated until the completion handler is called.

Declaration

Objective-C

- (void)setContext:(nonnull NSData *)context
completionHandler:(void (^_Nullable)(NSError *_Nullable))completion;

Swift

func setContext(_ context: Data, completionHandler completion: ((Error?) -> Void)? = nil)

Parameters

context

The new context to associate with the offline pack.

completion

The completion handler to call when the context has been updated. If there is an error setting the context, the error is passed into the completion handler.

state

The pack’s current state.

The state of an inactive or completed pack is computed lazily and is set to MGLOfflinePackStateUnknown by default. To request the pack’s status, use the -requestProgress method. To get notified when the state becomes known and when it changes, observe KVO change notifications on this pack’s state key path. Alternatively, you can add an observer for MGLOfflinePackProgressChangedNotifications about this pack that come from the default notification center.

Declaration

Objective-C

@property (nonatomic, readonly) MGLOfflinePackState state;

Swift

var state: MGLOfflinePackState { get }

progress

The pack’s current progress.

The progress of an inactive or completed pack is computed lazily, and all its fields are set to 0 by default. To request the pack’s progress, use the -requestProgress method. To get notified when the progress becomes known and when it changes, observe KVO change notifications on this pack’s state key path. Alternatively, you can add an observer for MGLOfflinePackProgressChangedNotifications about this pack that come from the default notification center.

Declaration

Objective-C

@property (nonatomic, readonly) MGLOfflinePackProgress progress;

Swift

var progress: MGLOfflinePackProgress { get }

-resume

Resumes downloading if the pack is inactive.

When a pack resumes after being suspended, it may begin by iterating over the already downloaded resources. As a result, the progress structure’s countOfResourcesCompleted field may revert to 0 before rapidly returning to the level of progress at the time the pack was suspended.

To temporarily suspend downloading, call the -suspend method.

Declaration

Objective-C

- (void)resume;

Swift

func resume()

-suspend

Temporarily stops downloading if the pack is active.

A pack suspends asynchronously, so some network requests may be sent after this method is called. Regardless, the progress property will not be updated until -resume is called.

If the pack previously reached a higher level of progress before being suspended, it may wait to suspend until it returns to that level.

To resume downloading, call the -resume method.

Declaration

Objective-C

- (void)suspend;

Swift

func suspend()

-requestProgress

Request an asynchronous update to the pack’s state and progress properties.

The state and progress of an inactive or completed pack are computed lazily. If you need the state or progress of a pack whose state property is currently set to MGLOfflinePackStateUnknown, observe KVO change notifications on this pack’s state key path, then call this method. Alternatively, you can add an observer for MGLOfflinePackProgressChangedNotification about this pack that come from the default notification center.

Declaration

Objective-C

- (void)requestProgress;

Swift

func requestProgress()
iOS SDK

SDK JS Reference

On this page