我有一个可与服务器一起使用的应用程序。我想创建一个离线模式,即用户创建某些东西或更改的东西,然后将其保存在某个地方,然后出现Internet连接时 - 这些请求转到服务器。我该如何实现?如何将块存储在NSUserDefaults
中?如何不为这些块失去指针?它可以是不同的控制器。请帮帮我。我为我的英语不好道歉。我尝试将此对象保存在NSUserDefaults
typedef void (^ExecutionBlock)(void);
@interface OfflineBlockObject : NSObject
@property (nonatomic, copy) ExecutionBlock block;
@property (nonatomic, strong) NSArray<NSMutableArray *> *operandsArray;
@property (nonatomic, strong) NSArray *conditionsArray;
@end
OfflineBlockObject *blockObject = [[OfflineBlockObject alloc] init];
BOOL first = [self.reservation.reservationID boolValue];
NSArray *conditions = @[@(first), @(self.shouldCallSetTagsForReservation)];
NSArray *operands = @[@[[self.reservation json], self.reservation.reservationID ? : @0, self.selectedTags ? : @[]],
@[[self.reservation json], @(self.shouldForceApproved), self.selectedTags? : @[]]];
blockObject.conditionsArray = [conditions copy];
blockObject.operandsArray = [operands copy];
@weakify(blockObject);
blockObject.block = [^{
@strongify(blockObject);
if ([blockObject.conditionsArray[0] boolValue]) {
ReservationsModel *m = [[ReservationsModel alloc] init];
[m editReservation:blockObject.operandsArray[0][0] success:^(id responseObject) {
if ([blockObject.conditionsArray[1] boolValue]) {
[m setReservationTags:blockObject.operandsArray[0][1] tags:blockObject.operandsArray[0][2] success:nil failure:nil];
}
} failure:nil];
} else {
self.reservation.eventId = self.reservationEvent.eventInfoID;
ReservationsModel *m = [[ReservationsModel alloc] init];
[m createReservation:blockObject.operandsArray[1][0] shouldForceApproved:[blockObject.operandsArray[1][1] boolValue] success:^(id responseObject) {
Reservation *reservation = [Reservation reservationWithJson:responseObject];
if (reservation.reservationID) {
if ([blockObject.conditionsArray[1] boolValue]) {
[m setReservationTags:reservation.reservationID tags:blockObject.operandsArray[0][2] success:nil failure:nil];
}
}
} failure:nil];
}
} copy];
[[OfflineQueueHelper sharedHelper] addTask:blockObject];
但是我的块属性变为null :(
,所以我有一个做到这一点的POD,但是它在迅速(OBJ -C使界面太过令人费解( - https://cocoapods.orgs.org/pods/offlineerequestmanager。您可以制作任何符合OfflineRequest的对象,以包装您要将其送入服务器的任何网络请求。如果您想确保通过应用程序终止持续存在,则还可以提供将其写入磁盘的字典。我们已经在内部应用程序中使用了一段时间了,因此希望其他人可以从中使用一些。
最简单的用例将看起来像以下内容,尽管大多数实际情况(保存到磁盘,特定的请求数据等(将有更多的箍可以跳过:
import OfflineRequestManager
class SimpleRequest: OfflineRequest {
func perform(completion: @escaping (Error?) -> Void) {
doMyNetworkRequest(withCompletion: { response, error in
handleResponse(response)
completion(error)
})
}
}
///////
OfflineRequestManager.defaultManager(queueRequest: SimpleRequest())