我有一个适用于服务器的应用程序。
我想创建一个离线模式,即当用户创建某些内容或更改它在某处保存时,然后当出现互联网连接时 - 这些请求将转到服务器。我怎样才能做到这一点?如何在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:(
答案 0 :(得分:0)
所以我有一个可以做到这一点的吊舱,但它在Swift中(Obj-C让界面太复杂了) - https://cocoapods.org/pods/OfflineRequestManager。您可以创建符合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())