首页 文章 精选 留言 我的

精选列表

搜索[国产化替换],共10008篇文章
优秀的个人博客,低调大师

Android 面试(六):你已经用 SharedPrefrence 的 apply() 替换 commit() 了吗?

这是 面试系列 的第六期。本期我们将来探讨一个有趣的东西 —— SharePrefrence 的两种提交方式 apply() 和 commit()。 往期内容传递:Android 面试(一):说说 Android 的四种启动模式Android 面试(二):如何理解 Activity 的生命周期Android 面试(三):用广播 BroadcastReceiver 更新 UI 界面真的好吗?Android 面试(四):Android Service 你真的能应答自如了吗?Android 面试(五):探索 Android 的 Handler 开始 其实非常有趣,我们经常在开发中使用 SharePrefrence 保存一些轻量级数据,比如判断是否是首次启动,首次启动进入引导页,否则直接到主页面,或者是其它的一些应用场景。 而我们也耳熟能详这样的写法。 根据 Context 获取 SharedPreferences 对象 利用 edit() 方法获取 Editor 对象。 通过 Editor 对象存储 key-value 键值对数据。 通过 commit() 方法提交数据。 public class SplashActivity extends AppCompatActivity { public static final String SP_KEY = "com.zxedu.myapplication.SplashActivity_SP_KEY"; public static final String IS_FIRST_IN = "com.zxedu.myapplication.SplashActivity_IS_FIRST_IN"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 一些业务代码 ...... SharedPreferences preferences = getSharedPreferences("name",MODE_PRIVATE); if (preferences.getBoolean(IS_FIRST_IN,true)){ // 跳转引导页面 startActivity(new Intent(this,GuideActivity.class)); finish(); }else{ // 跳转主页面 startActivity(new Intent(this,MainActivity.class)); } } } public class GuideActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); getSharedPreferences(SplashActivity.SP_KEY,MODE_PRIVATE).edit().putBoolean(SplashActivity.IS_FIRST_IN,false).apply(); } } 从代码中可以看到,一阵混乱操作,没啥特别的地方,但早期开发的人员应该知道,之前我们都是比较青睐 commit() 进行提交的。而现在 Android Studio 在我们使用 commit() 直接提交的时候会直接报黄色警告。 nanchen commit() 和 apply() 到底有什么异同? 先说说相同点: 二者都是提交 Prefrence 修改数据; 二者都是原子过程。 不同点直接上源码吧,先看看 commit() 方法的定义: /** * Commit your preferences changes back from this Editor to the * {@link SharedPreferences} object it is editing. This atomically * performs the requested modifications, replacing whatever is currently * in the SharedPreferences. * * <p>Note that when two editors are modifying preferences at the same * time, the last one to call commit wins. * * <p>If you don't care about the return value and you're * using this from your application's main thread, consider * using {@link #apply} instead. * * @return Returns true if the new values were successfully written * to persistent storage. */ boolean commit(); 综合一下 commit() 方法的注释也就是两点: 会返回执行结果。 如果不考虑结果并且是在主线程执行可以考虑 apply()。 再看看 apply() 方法的定义: /** * Commit your preferences changes back from this Editor to the * {@link SharedPreferences} object it is editing. This atomically * performs the requested modifications, replacing whatever is currently * in the SharedPreferences. * * <p>Note that when two editors are modifying preferences at the same * time, the last one to call apply wins. * * <p>Unlike {@link #commit}, which writes its preferences out * to persistent storage synchronously, {@link #apply} * commits its changes to the in-memory * {@link SharedPreferences} immediately but starts an * asynchronous commit to disk and you won't be notified of * any failures. If another editor on this * {@link SharedPreferences} does a regular {@link #commit} * while a {@link #apply} is still outstanding, the * {@link #commit} will block until all async commits are * completed as well as the commit itself. * * <p>As {@link SharedPreferences} instances are singletons within * a process, it's safe to replace any instance of {@link #commit} with * {@link #apply} if you were already ignoring the return value. * * <p>You don't need to worry about Android component * lifecycles and their interaction with <code>apply()</code> * writing to disk. The framework makes sure in-flight disk * writes from <code>apply()</code> complete before switching * states. * * <p class='note'>The SharedPreferences.Editor interface * isn't expected to be implemented directly. However, if you * previously did implement it and are now getting errors * about missing <code>apply()</code>, you can simply call * {@link #commit} from <code>apply()</code>. */ void apply(); 略微有点长,大概意思就是 apply() 跟 commit() 不一样的地方是,它使用的是异步而不是同步,它会立即将更改提交到内存,然后异步提交到硬盘,并且如果失败将没有任何提示。 总结一下不同点: commit() 是直接同步地提交到硬件磁盘,因此,多个并发的采用 commit() 做提交的时候,它们会等待正在处理的 commit() 保存到磁盘后再进行操作,从而降低了效率。而 apply() 只是原子的提交到内容,后面再调用 apply() 的函数进行异步操作。 翻源码可以发现 apply() 返回值为 void,而 commit() 返回一个 boolean 值代表是否提交成功。 apply() 方法不会有任何失败的提示。 那到底使用 commit() 还是 apply()? 大多数情况下,我们都是在同一个进程中,这时候的 SharedPrefrence 都是单实例,一般不会出现并发冲突,如果对提交的结果不关心的话,我们非常建议用 apply() ,当然需要确保操作成功且有后续操作的话,还是需要用 commit() 的。 做不完的开源,写不完的矫情。欢迎扫描下方二维码或者公众号搜索「nanchen」关注我的微信公众号,目前多运营 Android ,尽自己所能为你提升。如果你喜欢,为我点赞分享吧~ nanchen

优秀的个人博客,低调大师

监控告警平台的国产化选择—Rancher与夜莺的集成

作者简介 张智博,SUSE Rancher 大中华区研发总监,一直活跃在研发一线,经历了 OpenStack 到 Kubernetes 的技术变革,在底层操作系统 Linux、虚拟化 KVM 和 Docker 容器技术领域都有丰富的研发和实践经验。 通常提到在 Kubernetes 集群中搭建监控告警平台,普遍的选择都是 Prometheus,这源于 Prometheus 早期与 Kubernetes 的不断演进以及后续良好发展的生态。实际上,我们有很多不错的选择,尤其是一些国内优秀团队的作品。本文以 Rancher 与夜莺的整合为视角,为社区用户提供一些新的线索,这是打造一站式监控告警的另一个选择。 夜莺监控 (Nightingale) (https://github.com/ccfos/nightingale),是一款开源云原生监控分析系统,采用 All-In-One 的设计,集数据采集、可视化、监控告警、数据分析于一体,与云原生生态紧密集成,提供开箱即用的企业级监控分析能力。2022 年 5 月 11 日,夜莺监控项目赠予了中国计算机学会开源发展委员会(CCF ODC),成为该组织成立后接受捐赠的首个开源项目。 本文的集成实践遵循快速上手体验原则,通过 Rancher 部署 RKE 集群,并将夜莺的 Helm Chart 通过 Rancher UI 安装到该集群中。版本信息如下: Rancher v2.6.4 Nightingale Chart v0.1.0 RKE Kubernetes 1.22.9 Local Path Provisioner v0.0.22 Rancher 具备非常开放的整合能力,夜莺的 Helm Chart 可以非常方便地通过 Rancher Explorer UI 进行管理。安装 Rancher 并使用 RKE 引擎部署下游集群,在 Explorer UI 中切换到该集群,在 App&Marketplace 添加夜莺 Helm Chart Repo,刷新后可在 Charts 中找到 Nightingale: 夜莺的安装需要有 PVC 支持持久化数据,这里使用 Rancher 开发的一个轻量的 Local Path Provisioner,直接在当前集群中执行 kubectl shell 进行快速安装即可。 kubectl apply -f https://raw.githubusercontent.com/rancher/local-path-provisioner/v0.0.22/deploy/local-path-storage.yaml 夜莺的 Dashboard 支持多种访问方式,考虑到快速上手原则,这里使用 NodePort 暴露方式,在安装的 yaml 中,修改expose.type: nodePort。 等提示安装完成后,进入 Explorer UI 的 Service Discovery,使用 nightingale 过滤 Service,可以看到暴露的 NodePort 端口为 30007: 访问该 NodePort 端口,使用默认的超管用户(root/root.2020)即可登录到夜莺的 Dashboard: 由于夜莺 Helm Chart 会以 Daemonset 的形式,在 k8s 每个节点上启动了 telegraf 采集器,因此节点主机相关的监控指标,默认会自动被收集到夜莺系统中,只需要在夜莺 Web UI 导入内置的 Dashboard 和 Alert Rule,就可以很顺利的拿到可替代社区 Prometheus 的一站式监控告警平台。 导入内置 Dashboard: 导入内置的 Alert Rule: 在弹出的选择框中,选择导入 linux_by_telegraf,即可看到 Linux 主机相关的告警策略已经成功导入并且生效了。 夜莺的商业化支持目前由独立公司快猫星云(https://flashcat.cloud)负责。快猫星云秉承让监控分析变简单的初心和使命,依托其先进云原生监控分析平台,贯通监控数据要素,集数据采集、可视化、监控告警、数据分析于一体,为企业提供开箱即用的云原生监控分析能力。

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册