小编Jon*_*gat的帖子

为什么viewModelScope.launch默认运行在主线程上

在我学习协程以及如何在 android 应用程序中正确使用它们时,我发现了一些令我感到惊讶的事情。

在启动viewModelScope.launch { }lambda 中使用并设置断点启动协程时,我注意到我的应用程序不再响应,因为它仍在主线程上。

这让我感到困惑,因为文档viewModelScope.launch { }明确指出:

在不阻塞当前线程的情况下启动一个新的协程

当前线程不是主线程吗?如果默认情况下它不在不同的线程上运行,那么启动的全部目的是什么?

我能够viewModelScope.launch(Dispatchers.IO){ }在另一个线程上使用它按我的预期工作,即在另一个线程上运行它。

我试图通过该launch方法完成的是调用存储库并执行一些 IO 工作,即调用 Web 服务并将数据存储在房间数据库中。所以我想调用viewModelScope.launch(Dispatchers.IO){ }在不同的线程上完成所有工作,最后更新 LiveData 结果。

viewModelScope.launch(Dispatchers.IO){ liveData.postValue(someRepository.someWork()) }

所以我的第二个问题是,这是要走的路吗?

multithreading android coroutine android-architecture-components

18
推荐指数
3
解决办法
3502
查看次数

django模型验证不起作用

我有以下型号:

class CardInfo(models.Model):
    custid = models.CharField(max_length=30, db_index=True, primary_key = True)
    first_name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    street = models.CharField(max_length=100)
    building = models.CharField(max_length=100)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100)
    zipcode = models.CharField(max_length=100)
    payment_method = models.CharField(max_length=100, null=True, blank=True)
    amount = models.CharField(default = '0',max_length=10, null=True, blank=True)
    valid_to_month = models.CharField(max_length=100, null=True, blank=True)
    valid_to_year = models.CharField(max_length=100, null=True, blank=True)
def __unicode__(self):
    return "Cust ID %s" %(self.custid)
Run Code Online (Sandbox Code Playgroud)

在shell中,当我给full_clean我得到验证错误但是在保存它得到保存而不是抛出错误.为什么会这样?我正在使用django1.3和python 2.6:

c=CardInfo(custid="Asasasas")
c.full_clean()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/python2.6/lib/python2.6/site-packages/django/db/models/base.py", line 828, in …
Run Code Online (Sandbox Code Playgroud)

django django-models

10
推荐指数
1
解决办法
2930
查看次数

Jackson2ObjectMapperBuilder启用字段可见性ANY

我正在使用spring-boot并希望自定义创建的ObjectMapper.

我想要做的是能够序列化没有getter或setter的对象.在此之前可以通过在ObjectMapper上放置JsonAutoDetect.Visibility.ANY来完成.

但是如何使用我目前正在公开的Jackson2ObjectMapperBuilder bean启用此功能呢?

java spring jackson spring-boot

7
推荐指数
2
解决办法
3623
查看次数

django-taggit深度关系查询

我正在使用django-taggit,并且在尝试过滤关系时遇到了问题.

拥有以下型号:

class Artist(models.Model):
     tags = TaggableManager()


class Gig(models.Model):
    artist = models.ManyToManyField(Artist)
Run Code Online (Sandbox Code Playgroud)

我想要实现的是获得所有艺术家都有特定标签的演出.

我认为这很容易并急切地写道:

Gig.objects.filter(artist__tags__name__in=["rock"])
Run Code Online (Sandbox Code Playgroud)

哪个给了我:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/manager.py", line  141, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/query.py", line 550, in filter
  return self._filter_or_exclude(False, *args, **kwargs)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/query.py", line 568, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1172, in add_q
can_reuse=used_aliases, force_having=force_having)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1139, in add_filter
process_extras=False)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1060, in add_filter
negate=negate, process_extras=process_extras)
File "/home/jonas/.virtualenvs/wsw/lib/python2.7/site-packages/django/db/models/sql/query.py", line …
Run Code Online (Sandbox Code Playgroud)

django django-orm django-taggit

5
推荐指数
1
解决办法
1083
查看次数

spring-data @Query映射结果问题

我创建了一个扩展CrudRepository的Repository,这个存储库有一个带@Query符号的方法:

码:

@Query("select itemType, count(*) as count from Item where  User_id = :userId group by itemType")
List<Map<String, Long>> countItemsForUser(@Param("userId") Long userId);
Run Code Online (Sandbox Code Playgroud)

我遇到的问题是返回一个Object的ArrayList而不是Map的List.我已经读过JPA无法返回Map的地方,这就是我将结果填入List>的原因.

我不知道解决此问题或快速访问结果数据的最佳方法是什么.我已经尝试过铸造,但这也没有成功:

for(Object item: items) {
    Map<String,Long> castedItem = (HashMap<String,Long>)item;
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-data

5
推荐指数
1
解决办法
4748
查看次数

django-pipeline 不编译 sass 文件

我正在使用 django-1.3 与 django-staticfiles-1.2.1 和 django-pipeline-1.2.6 此设置应该根据文档进行工作。

在我的项目的根目录中,我有一个目录staticfiles,其中包含一个包含我的 sass 文件的目录sass。我希望看到 django-pipeline 编译我的 sass 文件并将它们放入 /static/css/master.css

这是我的 settings.py 文件的摘录

MEDIA_ROOT = '/home/jonasg/dev/projectX/media/' 

STATIC_ROOT = 'static/'
STATIC_URL = '/static/'   
PIPELINE=True
PIPELINE_AUTO=True
STATICFILES_STORAGE = 'pipeline.storage.PipelineStorage'
STATICFILES_DIRS = ( 
    'staticfiles',
    )   
PIPELINE_COMPILERS = ( 
      'pipeline.compilers.sass.SASSCompiler',
      )   
PIPELINE_CSS_COMPRESSOR = 'pipeline.compressors.cssmin.CssminCompressor'

PIPLINE_CSS = { 
    'base': {
        'source_filenames': (
            'sass/*.sass'
            ),                                                                                                                                    
        'output_filename': 'css/master.css'
        }   
    }   
PIPELINE_COMPILERS = ( 
      'pipeline.compilers.sass.SASSCompiler',
      )   
PIPELINE_SASS_BINARY='/usr/bin/sass'
STATICFILES_FINDERS = ( 
         'staticfiles.finders.FileSystemFinder',
         'staticfiles.finders.AppDirectoriesFinder',
         'staticfiles.finders.DefaultStorageFinder'
      )   
Run Code Online (Sandbox Code Playgroud)

当我运行 ./manage.pycollectstatic 时,/staticfiles 中的所有文件都会复制到/static 中,但不会编译或缩小任何内容。我还注意到这个命令从 …

python django sass

5
推荐指数
1
解决办法
3747
查看次数

spring-data-rest没有与spring-mvc完全集成

我正在使用spring-data-rest与现有的spring-mvc应用程序.

我正在使用spring-data-rest的2.0.0.M1版本.

我使用xml设置spring-data-rest:

    <bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration" />
Run Code Online (Sandbox Code Playgroud)

这是stakctrace的摘录:

Unsatisfied dependency expressed through constructor argument with index 7 of type [org.springframework.data.rest.core.support.DomainObjectMerger]: : Error creating bean with name 'domainObjectMerger' defined in class path resource [org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.data.rest.core.support.DomainObjectMerger org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration.domainObjectMerger() throws java.lang.Exception] threw exception; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/mapping/SimplePropertyHandler;
Run Code Online (Sandbox Code Playgroud)

所以我从这里看到的是无法创建DomainObjectMerger,因为无法找到org/springframework/data/mapping/SimplePropertyHandler.

启动应用程序时,我得到以下堆栈跟踪:

ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'repositoryEntityController' defined in URL [jar:file:/Users/jonas/Documents/workspace-sts-3.4.0.RELEASE/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/ququ/WEB-INF/lib/spring-data-rest-webmvc-2.0.0.M1.jar!/org/springframework/data/rest/webmvc/RepositoryEntityController.class]: Unsatisfied dependency expressed through constructor argument with …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-data-rest

5
推荐指数
1
解决办法
8675
查看次数

CQRS如何为更具伸缩性的应用程序做出贡献

最近我一直在阅读CQRS架构.关于为什么应该使用CQRS的最重要的一点是可扩展性.

现在我不太明白这是如何工作的.

假设您拥有typicalCQRS应用程序设计.

  • 两个数据存储区
  • 一个用于指挥方
  • 一个用于查询端
  • 处理Command时,会发送一个可以更新第二个数据存储区的事件

经常声明,拥有一个用于查询的数据存储区和一个用于处理命令的数据存储区将使您的应用程序更具可伸缩性.但是,如果存储事件数据的第二个数据存储需要响应查询请求并且还需要根据传入事件不断更新自身,那怎么能工作呢?

为什么没有一个存储命令的数据存储区以及查询方可以重新使用存储数据来获取结果数据的位置?

domain-driven-design cqrs

5
推荐指数
1
解决办法
549
查看次数

模拟补丁多个

我试图从测试用例中模拟模块内的多个函数:

from mock import patch, DEFAULT, Mock

function_a = Mock()
function_a.return_value = ['a', 'list'] 
with patch.multiple('target.module',
                    function_a=function_a,
                    function_b=DEFAULT) as (f_a, f_b):
Run Code Online (Sandbox Code Playgroud)

令我惊讶的是,这不起作用,给了我以下回溯:

ValueError: need more than 1 value to unpack
Run Code Online (Sandbox Code Playgroud)

使用: http: //www.voidspace.org.uk/python/mock/

python unit-testing mocking

4
推荐指数
1
解决办法
7567
查看次数

在测试用例中定义弹簧活动配置文件

使用Spring 4,我有以下测试设置:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = JpaConfig.class)
@ActiveProfiles(resolver = TestResolver.class)
public class SimpleTest {
Run Code Online (Sandbox Code Playgroud)

TestResolver已实现为:

public class TestResolver implements ActiveProfilesResolver {
    @Override
    public String[] resolve(Class<?> aClass) {
        String[] profiles = new String[1];
        profiles[0] = "test";
        return profiles;
    }
}
Run Code Online (Sandbox Code Playgroud)

JpaConfig已经使用PropertySource注释

@Configuration
@PropertySource("classpath:properties/application-${spring.profiles.active:dev}.properties")
@EnableJpaRepositories(basePackages={"com.my.namespace.repositories"})
public class JpaConfig {
Run Code Online (Sandbox Code Playgroud)

每当我运行SimpleTest时,它会尝试找到:properties/application-dev.properties,而我期望它是properties/application-test.properties.

我在这里要完成的是基于以下帖子:使用profile进行Spring集成测试

java spring spring-test

4
推荐指数
1
解决办法
8955
查看次数