在我的应用程序中,我有一个名为FavoritesController的类,它管理用户标记为收藏夹的对象,然后在整个应用程序中使用此收藏状态。 FavoritesController被设计为单例类,因为整个应用程序中有许多UI元素需要知道最喜欢的状态'对于不同地方的对象,网络请求也需要能够发出信号,如果服务器这样说,则需要使收藏失效。
当服务器响应404错误时,会发生此无效部分,表示必须从用户的收藏夹中删除收藏对象。网络提取函数抛出一个错误,触发FavoritesController删除对象,然后向感兴趣的各方发送需要刷新的通知。
当使用单元测试来检查404实现的质量时,所有方法都按预期触发 - 抛出并捕获错误,FavoritesController删除对象并发送通知。但在某些情况下,删除的收藏夹仍然存在 - 但这取决于查询的完成位置!
如果我在单例内查询,删除就没问题,但是如果我从使用单例的类中查询,则删除没有发生。
favorites
使用包含所有访问@synchronized()
的ivar,并且ivar的值由NSUserDefaults属性支持。id
和name
。一个奇怪的事情我无法理解为什么会发生这种情况:在一些删除尝试中,收藏对象的name
值设置为""
但id
键保留其价值。
我编写的单元测试添加了一个无效的收藏夹并检查它是否在第一次服务器查询时被删除。当从空的收藏夹开始时,该测试通过,但是当存在“半删除”的实例时失败。对象如上(保留其 id
值)
sharedController
总是返回相同的实例。FavoritesController主要方法
- (void) serverCanNotFindFavorite:(NSInteger)siteID {
NSLog(@"Server can't find favorite");
NSDictionary * removedFavorite = [NSDictionary dictionaryWithDictionary:[self favoriteWithID:siteID]];
NSUInteger index = [self indexOfFavoriteWithID:siteID];
[self debugLogFavorites];
dispatch_async(dispatch_get_main_queue(), ^{
[self removeFromFavorites:siteID completion:^(BOOL success) {
if (success) {
NSNotification * note = [NSNotification notificationWithName:didRemoveFavoriteNotification object:nil userInfo:@{@"site" : removedFavorite, @"index" : [NSNumber numberWithUnsignedInteger:index]}];
NSLog(@"Will post notification");
[self debugLogFavorites];
[self debugLogUserDefaultsFavorites];
[[NSNotificationCenter defaultCenter] postNotification:note];
NSLog(@"Posted notification with name: %@", didRemoveFavoriteNotification);
}
}];
});
}
- (void) removeFromFavorites:(NSInteger)siteID completion:(completionBlock) completion {
if ([self isFavorite:siteID]) {
NSMutableArray * newFavorites = [NSMutableArray arrayWithArray:self.favorites];
NSIndexSet * indices = [newFavorites indexesOfObjectsPassingTest:^BOOL(NSDictionary * entryUnderTest, NSUInteger idx, BOOL * _Nonnull stop) {
NSNumber * value = (NSNumber *)[entryUnderTest objectForKey:@"id"];
if ([value isEqualToNumber:[NSNumber numberWithInteger:siteID]]) {
return YES;
}
return NO;
}];
__block NSDictionary* objectToRemove = [[newFavorites objectAtIndex:indices.firstIndex] copy];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"Will remove %@", objectToRemove);
[newFavorites removeObject:objectToRemove];
[self setFavorites:[NSArray arrayWithArray:newFavorites]];
if ([self isFavorite:siteID]) {
NSLog(@"Failed to remove!");
if (completion) {
completion(NO);
}
} else {
NSLog(@"Removed OK");
if (completion) {
completion(YES);
}
}
});
} else {
NSLog(@"Tried removing site %li which is not a favorite", (long)siteID);
if (completion) {
completion(NO);
}
}
}
- (NSArray *) favorites
{
@synchronized(self) {
if (!internalFavorites) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
self->internalFavorites = [self.defaults objectForKey:k_key_favorites];
});
if (!internalFavorites) {
internalFavorites = [NSArray array];
}
}
return internalFavorites;
}
}
- (void) setFavorites:(NSArray *)someFavorites {
@synchronized(self) {
internalFavorites = someFavorites;
[self.defaults setObject:internalFavorites forKey:k_key_favorites];
}
}
- (void) addToFavorites:(NSInteger)siteID withName:(NSString *)siteName {
if (![self isFavorite:siteID]) {
NSDictionary * newFavorite = @{
@"name" : siteName,
@"id" : [NSNumber numberWithInteger:siteID]
};
dispatch_async(dispatch_get_main_queue(), ^{
NSArray * newFavorites = [self.favorites arrayByAddingObject:newFavorite];
[self setFavorites:newFavorites];
});
NSLog(@"Added site %@ with id %ld to favorites", siteName, (long)siteID);
} else {
NSLog(@"Tried adding site as favorite a second time");
}
}
- (BOOL) isFavorite:(NSInteger)siteID
{
@synchronized(self) {
NSNumber * siteNumber = [NSNumber numberWithInteger:siteID];
NSArray * favs = [NSArray arrayWithArray:self.favorites];
if (favs.count == 0) {
NSLog(@"No favorites");
return NO;
}
NSIndexSet * indices = [favs indexesOfObjectsPassingTest:^BOOL(NSDictionary * entryUnderTest, NSUInteger idx, BOOL * _Nonnull stop) {
if ([[entryUnderTest objectForKey:@"id"] isEqualToNumber:siteNumber]) {
return YES;
}
return NO;
}];
if (indices.count > 0) {
return YES;
}
}
return NO;
}
FavoritesController的Singleton实现
- (instancetype) init {
static PKEFavoritesController *initedObject;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
initedObject = [super init];
self.defaults = [NSUserDefaults standardUserDefaults];
});
return initedObject;
}
+ (instancetype) sharedController
{
return [self new];
}
单元测试代码
func testObsoleteFavoriteRemoval() {
let addToFavorites = self.expectation(description: "addToFavorites")
let networkRequest = self.expectation(description: "network request")
unowned let favs = PKEFavoritesController.shared()
favs.clearFavorites()
XCTAssertFalse(favs.isFavorite(313), "Should not be favorite initially")
if !favs.isFavorite(313) {
NSLog("Adding 313 to favorites")
favs.add(toFavorites: 313, withName: "Skatås")
}
let notification = self.expectation(forNotification: NSNotification.Name("didRemoveFavoriteNotification"), object: nil) { (notification) -> Bool in
NSLog("Received notification: \(notification.name.rawValue)")
return true
}
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
NSLog("Verifying 313 is favorite")
XCTAssertTrue(favs.isFavorite(313))
addToFavorites.fulfill()
}
self.wait(for: [addToFavorites], timeout: 5)
NSLog("Will trigger removal for 313")
let _ = SkidsparAPI.fetchRecentReports(forSite: 313, session: SkidsparAPI.session()) { (reports) in
NSLog("Network request completed")
networkRequest.fulfill()
}
self.wait(for: [networkRequest, notification], timeout: 10)
XCTAssertFalse(favs.isFavorite(313), "Favorite should be removed after a 404 error from server")
}
答案 0 :(得分:1)
为了给出我的答案的上下文,这是建议更改时所讨论的代码:
- (NSArray *)favorites {
@synchronized(internalFavorites) {
if (!internalFavorites) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
internalFavorites = [self.defaults objectForKey:k_key_favorites];
});
if (!internalFavorites) {
internalFavorites = [NSArray array];
}
}
}
return internalFavorites;
}
我对if (!internalFavorites) {
之后的支票@synchronized(internalFavorites)
持怀疑态度,因为这意味着期待@synchronized
被传递nil
,results in a noop。
这意味着对favorites
或setFavorites
的多次调用可能会以有趣的方式发生,因为它们实际上不会同步。赋予@sychronized
实际对象以进行同步对于线程安全至关重要。同步自我很好,但是对于特定的类,你必须小心不要在自己上同步太多的东西,否则你必然会创建不必要的阻塞。向NSObject
提供简单的@sychronized
是缩小您保护范围的好方法。
以下是您可以避免使用self
锁定的方法。
- (instancetype)init {
static PKEFavoritesController *initedObject;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
initedObject = [super init];
self.lock = [NSObject new];
self.defaults = [NSUserDefaults standardUserDefaults];
});
return initedObject;
}
+ (instancetype)sharedController {
return [self new];
}
- (NSArray *)favorites {
@synchronized(_lock) {
if (!internalFavorites) {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
self->internalFavorites = [self.defaults objectForKey:k_key_favorites];
});
if (!internalFavorites) {
internalFavorites = [NSArray array];
}
}
}
return internalFavorites;
}
关于测试运行之间的异常,明确调用synchronize
上的NSUserDefaults
将有所帮助,因为更改默认值的调用是异步的,这意味着涉及其他线程。还有3层缓存,特别是为了运行测试synchronize
,应该确保在Xcode在测试运行时拔出插件之前完全干净地提交。文件非常突然地坚持认为这不是一个必要的电话,但如果它真的没有必要就不存在:-)。在我的第一个iOS项目中,我们总是在每次默认更改后调用synchronize
...所以,我认为文档在Apple工程师的部分更有抱负。我很高兴这种直觉帮助了你。