V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  summer1991  ›  全部回复第 1 页 / 共 2 页
回复总数  21
1  2  
2017-07-15 11:00:19 +08:00
回复了 summer1991 创建的主题 问与答 求问!
@shuirong1997 尴尬
2017-07-03 16:15:11 +08:00
回复了 summer1991 创建的主题 问与答 请教一个客户端编程的问题
@KNOX 如果刷新局部 你还要维护是否变化 以及哪些东西变了。这样就已经使问题复杂化了。如果我和这个人除了关注关系 比如再加一个点赞 局部刷新的逻辑就更复杂了
2017-07-03 11:37:26 +08:00
回复了 summer1991 创建的主题 问与答 请教一个客户端编程的问题
@blacklee 所以你在内存中维护一个我关注的人的 list 然后用 rac ?
2017-07-03 11:36:49 +08:00
回复了 summer1991 创建的主题 问与答 请教一个客户端编程的问题
@KNOX。。。这不就是回调了一下么 跟返回时刷新没有什么区别
2017-07-03 11:35:37 +08:00
回复了 summer1991 创建的主题 问与答 请教一个客户端编程的问题
@tmac6740 你是指本地维护 list 还是网络请求 list ?
@winglight2016 有好的想法吗亲?
@winglight2016 可以这么说,需要重用方法,并且这个方法里面能和 VC 一样,能弹窗,能发起网络请求
2017-07-03 11:29:46 +08:00
回复了 summer1991 创建的主题 iOS 大家好,我又来请教了
@wohenyingyu02 你说的是一种方法 解决方法 1.出来时刷新 (比较浪费网络) 2。 本地维护一个关注的人的列表 (本地处理比较复杂) 有没有简单的方法
2017-07-03 11:27:54 +08:00
回复了 summer1991 创建的主题 iOS 大家好,我又来请教了
@LINAICAI 我举的是一个场景 如果这种情况多了 代码的通知会乱飞 基本处于不可控的状态
2017-07-03 11:26:20 +08:00
回复了 summer1991 创建的主题 iOS 大家好,我又来请教了
@LINAICAI 这样的话 应用内得有多少通知啊 ? 同时应用的内存数据中得存多少数据?
@ichanne 恩 我又参考了下 sunnyx 的博客,感觉这个方法可以试一试 http://blog.sunnyxx.com/2015/12/19/self-manager-pattern-in-ios/
@xiubin 你的方案并没有解决问题 :-(
@ichanne 这样不是把 View 和 Model,网络 这些杂糅在一起吗?
求解答!
@andyL 可能之前没有描述好,大家都抓着代码细节看 我整理了下代码放在 https://www.v2ex.com/t/368630, 你可以再看看
```Objective-c
//版本 v2

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//store/pref/ASHPrefStore.h

@interface ASHPrefStore : NSObject

+ (BOOL)haveReadBroadcastTip;
+ (void)setHaveReadBroadcastTip;

@end

//store/pref/ASHPrefStore.m

@implementation ASHPrefStore

static NSString *const kHaveReadBroadcastTipKey = @"haveReadBroadcastTip";

+ (BOOL)haveReadBroadcastTip
{
return [[NSUserDefaults standardUserDefaults] boolForKey:kHaveReadBroadcastTipKey];
}

+ (void)setHaveReadBroadcastTip
{
[[NSUserDefaults standardUserDefaults] setObject:@(YES) forKey:kHaveReadBroadcastTipKey];
}

@end

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//network/ASHNetworkAPI.h

@interface ASHNetworkAPI : NSObject

+ (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock;

@end

//network/ASHNetworkAPI.m

@implementation ASHNetworkAPI

+ (void)sendBroadcastRequestWithMsg:(NSString *)msg successBlock:(void(^)(void))successBlock failureBlock:(void(^)(NSError *error))failureBlock
{
//send request
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
!successBlock ?: successBlock();
});
}

@end

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//utils/ui/ASHAlertUtils.h

@interface ASHAlertUtils : NSObject

+ (void)showAlertWithController:(UIViewController *)controller msg:(NSString *)msg callback:(void(^)(void))callback;

+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback;

@end

//utils/ui/ASHAlertUtils.m

@implementation ASHAlertUtils

#pragma mark - utils

+ (void)showAlertWithController:(UIViewController *)controller msg:(NSString *)msg callback:(void(^)(void))callback
{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
!callback ?: callback();
}]];
[controller presentViewController:alert animated:YES completion:nil];
}

+ (void)showEditAlertWithController:(UIViewController *)controller title:(NSString *)title placeholder:(NSString *)placeholder callback:(void(^)(NSString *content))callback
{
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
textField.placeholder = placeholder;
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"发送" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *content = [[alertController textFields][0] text];
!callback ?: callback(content);
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"Canelled");
}]];
[controller presentViewController:alertController animated:YES completion:nil];
}

@end

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//macro.h

#ifndef weakify
#if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x;
#else // #if __has_feature(objc_arc)
#define weakify( x ) autoreleasepool{} __block __typeof__(x) __block_##x##__ = x;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef weakify

#ifndef normalize
#if __has_feature(objc_arc)
#define normalize( x ) try{} @finally{} __typeof__(x) x = __weak_##x##__;
#else // #if __has_feature(objc_arc)
#define normalize( x ) try{} @finally{} __typeof__(x) x = __block_##x##__;
#endif // #if __has_feature(objc_arc)
#endif // #ifndef @normalize

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//controller/ASHTempViewController.m

@implementation ASHTempViewController

#pragma mark - app logic

- (void)sendBroadcastWithSuccessBlock:(void (^)(void))successBlock
{
@weakify(self)
void (^showSendBroadcast) (void) = ^{
[ASHAlertUtils showEditAlertWithController:self title:@"发送广播'" placeholder:@"说点什么..." callback:^(NSString *content) {
[ASHNetworkAPI sendBroadcastRequestWithMsg:content successBlock:successBlock failureBlock:^(NSError *error) {
@normalize(self)
!self ?: [ASHAlertUtils showAlertWithController:self msg:error.localizedDescription callback:nil];
}];
}];
};

if ([ASHPrefStore haveReadBroadcastTip]) {
showSendBroadcast();
} else {
NSString *title = @"1、发布广播,全服玩家都可以看到;\n2、禁止发布反动、政治、色情、辱骂、广告等不良言论,否则将会遭到删除、封号处理。";
[ASHAlertUtils showAlertWithController:self msg:title callback:^{
[ASHPrefStore setHaveReadBroadcastTip];
showSendBroadcast();
}];
}
}
```
@blacklist 你还木有看清楚我的问题呀亲
@guomiaoyou7784 上边代码中网络请求的代码在网络请求模块,网络调用代码在 vc 中也只有 一行调用 + block 回调,放到 view model 中只是多增加了一层调用,除了更复杂好像没有什么优化。不同按钮的逻辑放在 viewController 的不同分类中,如果在这个逻辑处理中有一些状态需要放到 vc 中,岂不是很麻烦;而且我觉得分类这种形式使用来增强类本身的功能,如果这些逻辑放在这个类的分类中,且用于被这个类自身调用,是不是比较牵强。有具体的例子可以发出来吗?
@guomiaoyou7784 这个文章我以前看过的,但针对于代码中的上述情况,文章中说的做法没有一个是有效的。能不能将你的想法结合代码具体说明下?
@winglight2016 比如界面上有十个按钮,每个大概都是这个逻辑怎么办?
1  2  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2214 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 19ms · UTC 04:43 · PVG 12:43 · LAX 21:43 · JFK 00:43
Developed with CodeLauncher
♥ Do have faith in what you're doing.